Use ODBC to import data from CSV files to Dataset

Source: Internet
Author: User
Tags odbc connection

CSV files, commonly known as "comma-separated files". You can use iostream to read CSV files in a specified format...

I thought it was just this way, huh, huh, until one day, I saw the text connection on www.connectionstrings.com.

String:

Text

  • ODBC

    • Standard:

      "Driver = {Microsoft text Driver (*. txt; *. CSV)}; DBQ = 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 first row contains columnnames, not data

      • Standard:

        "Driver = {Microsoft text Driver (*. txt; *. CSV)}; DBQ = 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 first row contains columnnames, not data

      • Standard:

        "Provider = Microsoft. Jet. oledb.4.0; Data Source = c: \ txtfilesfolder \; extended properties =" "text; HDR = yes; FMt = delimited """

        "HDR = yes;" indicates that the first row contains columnnames, not data

Isn't there a CSV here? Oh, you can try it. The test results are very good. You can use ODBC to connect. Use CSV as a database,

It feels good. (oledb is not successful. If properties = text is changed to properties = CSV, It is not supported, or am I wrong ?)

Therefore, here is a method provided (not invented by me), using the ODBC connection method, directly using the dataadapter

Quick data import to dataset is convenient. The method is as follows:

Public dataset getdatasetfromcsv (string filepath, string filename)
{
String strconn = @ "driver = {Microsoft text Driver (*. txt; *. CSV)}; DBQ = ";
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.