Use ODBC to import data from a CSV file into a dataset

Source: Internet
Author: User
Tags contains odbc odbc connection ole
odbc| data

CSV files, commonly known as "comma-delimited files", the way to read the CSV file can be read by using iostream ...

I thought this one method, hehe. Until one day. See the connection of the text on the www.ConnectionStrings.com

String:

Text

  • Odbc

    • Standard:
      "Driver={microsoft Text Driver (*.txt; *.csv)};D bq=c:\txtfilesfolder\; Extensions=asc,csv,tab,txt; "
  • OLE DB

    • Standard:
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtfilesfolder\; Extended properties= "" text; Hdr=yes; Fmt=delimited "" ""
      hdr=yes; "indicates that
      " the "the" the "the" the "the" the "the". Contains ColumnNames


    • Standard:
      "Driver={microsoft Text Driver (*.txt; *.csv)};D bq=c:\txtfilesfolder\; Extensions=asc,csv,tab,txt; "
  • OLE DB

    • Standard:
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtfilesfolder\; Extended properties= "" text; Hdr=yes; Fmt=delimited "" ""
      hdr=yes; "indicates that
      " the "the" the "the" the "the" the "the". Contains ColumnNames


    • Standard:
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtfilesfolder\; Extended properties= "" text; Hdr=yes; Fmt=delimited "" ""
      hdr=yes; "indicates that
      " the "the" the "the" the "the" the "the". Contains ColumnNames

Isn't there a csv here? Oh, you can try Ah, the test results are very good. You can use ODBC to connect. csv as a database,

It feels good. (OLE DB's unsuccessful, will properties=text change to Properties=csv does not work, is not support, or I write wrong?)

So here's a way to provide. (Certainly not my invention OH), use ODBC connection way, through DataAdapter directly will

Data to quickly import dataset, very convenient. The method is as follows:

Public DataSet getdatasetfromcsv (string filePath, String fileName)
{
String strconn = @ "Driver={microsoft Text Driver (*.txt; *.csv)};D bq=";
strconn + = FilePath; FilePath, for Example:c:\
strconn + = "; Extensions=asc,csv,tab,txt; ";
OdbcConnection objconn = new OdbcConnection (strconn);
DataSet dscsv = new DataSet ();
Try
{
String strSQL = "SELECT * from" + fileName; FileName, for Example:1.csv
OdbcDataAdapter odbccsvdataadapter = new OdbcDataAdapter (strSQL, objconn);
Odbccsvdataadapter.fill (dscsv);
return dscsv;
}
catch (Exception ex)
{
Throw ex;
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.