none
insert csv file into the sqltable RRS feed

  • Question

  • I am writing a program by which it reads the data from the csv file and insert data into the sql table. and the program is running successfully. but i am getting one bug that it not reads the first row of the file.

    program:
           
            OdbcConnection cnx;
            System.Data.DataTable dt = new System.Data.DataTable();
            OdbcDataAdapter da = new OdbcDataAdapter();
            OdbcCommand cmd;
            string cnxStr;
            string CSVFolder;
            CSVFolder = "c:\\CSVFolder";
            cnxStr = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + CSVFolder + ";";
            cnx = new OdbcConnection(cnxStr);
            cmd = new OdbcCommand("Select * from [junk.txt] ", cnx);
            da.SelectCommand = cmd;
            cnx.Open();
            da.Fill(dt);
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                if (Convert.ToString(dr[0]).Length > 0 && Convert.ToString(dr[1]).Length > 0)
                {
                    //sql insertion command  here
                }
            }

    note: suppose if the file contains the three rows

    1, sudhir
    2, bharti
    3, raj

    it is counting the total row as 2, it leaves the first row (1, sudhir)

    and ideas?






    Friday, January 9, 2009 12:47 PM

All replies

  •  The first row is apparently treated as header row. I don't think that the ODBC connection string supports a parameter like "HDR=No" which would be valid for the Jet Oledb Provider when reading text files. You may however create a schema.ini file and save in the CSV folder. It should contain a definition for every text file in the folder, e.g.:

      [Junk.TXT]
       ColNameHeader = False
       Format = CSVDelimited
       CharacterSet = ANSI
       Col1=ID short
       Col2=Name char width 30


    Hope this helps.

    Ralf
    Tuesday, January 13, 2009 5:56 PM
  •  The first row is apparently treated as header row. I don't think that the ODBC connection string supports a parameter like "HDR=No" which would be valid for the Jet Oledb Provider when reading text files. You may however create a schema.ini file and save in the CSV folder. It should contain a definition for every text file in the folder, e.g.:

      [Junk.TXT]
       ColNameHeader = False
       Format = CSVDelimited
       CharacterSet = ANSI
       Col1=ID short
       Col2=Name char width 30


    Hope this helps.

    Ralf

    Great! It help me!
    Thursday, November 19, 2009 8:31 AM