C# CSV import schema.ini

2671 단어 schema

We often need to import data from a csv file.

    /// <summary>

       /// Normal task to get data set from csv.

       /// </summary>

       /// <param name="filepath"></param>

       /// <returns></returns>

       public DataSet GetDataSetFromCSV(string filepath)

       {

           string strconn = @"driver={microsoft text driver (*.txt; *.csv)};dbq=";

           strconn += Path.GetDirectoryName(filepath);

           strconn += ";extensions=csv;";

           using (OdbcConnection objconn = new OdbcConnection(strconn))

           {

               try

               {

                   DataSet dscsv = new DataSet();

                   string strsql = "select * from " + Path.GetFileName(filepath);

                   OdbcDataAdapter odbcDataAdapter = new OdbcDataAdapter(strsql, objconn);

                   odbcDataAdapter.Fill(dscsv);



                   return dscsv;

               }

               catch

               {

                   return null;

               }

           }

       }

Int Issure


When on column is like 0001, we will only get the 1 in the dataset for we treat it as int.
One solution to use the schema.ini.
[Hello.csv]

ColNameHeader=True

Format=CSVDelimited

MaxScanRows=0

CharacterSet=ANSI

Col1=ID Text 10

Col2=Name Text 10

One solution to add the quoto to these column
“001”
One solutin is to use FileStream to read the CSV file.

Encoding Issure


You can only use the ANSI and OEM CharacterSeT.
ANSI = Encoding.Default()
OEM is just for IBM
Note: 본문은 bnbqian 오리지널에 속합니다.

좋은 웹페이지 즐겨찾기