none
insert csv string in sql table RRS feed

  • Question

  • I am writing a program where my requirement is to insert the csv string format like the below

    FD,C3,30,01,00,53,27,05,7,0,5,0,119,50,0,0,0,0,157,15,121,17,0,0,0,115,33,127
    FD,C3,30,01,00,53,27,05,7,0,10,0,120,51,0,0,0,0,159,14,121,17,0,0,0,115,33,135
    FD,C3,30,01,00,53,27,05,7,0,15,0,122,50,0,0,0,0,160,14,121,17,0,0,0,115,33,142

    into the sql table in the respective columns.

    Program:

    string downloaddata=
    "FD,C3,30,01,00,53,27,05,7,0,5,0,119,50,0,0,0,0,157,15,121,17,0,0,0,115,33,127
    FD,C3,30,01,00,53,27,05,7,0,10,0,120,51,0,0,0,0,159,14,121,17,0,0,0,115,33,135
    FD,C3,30,01,00,53,27,05,7,0,15,0,122,50,0,0,0,0,160,14,121,17,0,0,0,115,33,142";

    char sep = ',';
    string[] values = downloaddata.Split(sep);

    Goal

    It convert all the item in single dimension array, but my requirement is to differentiate the line number, i.e two dimension array. (line number, item value)

    any ideas?

    Thursday, January 8, 2009 12:17 PM

Answers

  • Could you restate the goal more specifically in terms of what you expect?

    i.e. struct f { int lin, string val };
     f[] values = new f[] {
       f { lin=1, val="FD"}, .... , f {lin=1, val="127" },
       f { lin=2, val="FD"}, ... , f { lin=2, val="135" },
       f { lin=3, val="FD"}, ... , f { lin=2, val="142" },
    }

    List<f> values= new List<f>();
    int start =0;
    int line = 0;
    for(int i = 0; i < downloaddata.Length; ++i) {
      if (downloaddata[i] == '\n') {
        start = i+1;
        line++;
      }
      if (downloaddata[i] = ',' || downloaddata[i] == '\n') { // may need to filter out '\r'
         values.Add(new f() {  lin = line, val = downloaddata.Substring(start, i-start); });
         start = i+1;
      }
    }
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Friday, January 16, 2009 2:50 AM
    Moderator

All replies

  • You could first split the lines based on the NewLine (System.Environment.NewLine) character, and then split each line in the array by the comma.
    Ewald - Please remember to mark the replies as answers if they help.
    Thursday, January 8, 2009 2:54 PM
  • actually i am getting the complete string with the line break, so we can say this is the final output, so i can't implement the split function line by line.

    any other ideas?
    Thursday, January 8, 2009 2:56 PM
  • Could you restate the goal more specifically in terms of what you expect?

    i.e. struct f { int lin, string val };
     f[] values = new f[] {
       f { lin=1, val="FD"}, .... , f {lin=1, val="127" },
       f { lin=2, val="FD"}, ... , f { lin=2, val="135" },
       f { lin=3, val="FD"}, ... , f { lin=2, val="142" },
    }

    List<f> values= new List<f>();
    int start =0;
    int line = 0;
    for(int i = 0; i < downloaddata.Length; ++i) {
      if (downloaddata[i] == '\n') {
        start = i+1;
        line++;
      }
      if (downloaddata[i] = ',' || downloaddata[i] == '\n') { // may need to filter out '\r'
         values.Add(new f() {  lin = line, val = downloaddata.Substring(start, i-start); });
         start = i+1;
      }
    }
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Friday, January 16, 2009 2:50 AM
    Moderator