CSV to able (1)

Source: Internet
Author: User
In the two methods above, I prefer 2nd, which is simple, convenient, efficient, and error-prone. The following describes how to directly generate the source code of a CSV file using ODBC or oledb. Note the following two methods: ODBC or oledb adopts the Windows Default character encoding ANSI to read data, and the input stream encoding cannot be changed. Therefore, the CSV file output must adopt ANSI encoding, that is, encoding. default. (See datatable content output to CSV file). In addition, ODBC or oledb can only read CSV files in standard format (separated by commas ). In this case, the first method can be used. // Source code for reading CSV files in ODBC Mode Private void btnreadcsvbyodbc_click (Object sender, eventargs E)
{
This. opencvsfiledialog. Title = string. Format ("{0} Getting started successfully", "CSV cute ");
This. opencvsfiledialog. defaultext = "CSV ";
This. opencvsfiledialog. Filter = "CSV (カン) (*. CSV) | *. CSV ";
Dialogresult DR = This. opencvsfiledialog. showdialog (this );
If (DR = dialogresult. OK)
{
String strfilepathandname = opencvsfiledialog. filename;
String strconn = @ "driver = {driver da Microsoft para arquivos texto (*. txt ;*. CSV)}; DBQ = C:/Documents and Settings/Wang/My Documents ENTs; extensions = ASC, CSV, tab, txt ;";
// Filename, for example: 1.csv
String strsql = @ "select * from" + system. Io. Path. getfilename (strfilepathandname );
Odbcconnection objconn = new odbcconnection (strconn );
Dataset dscsv = new dataset ();
Try
{
Odbcdataadapter odbccsvdataadapter = new odbcdataadapter (strsql, objconn ); Odbccsvdataadapter. Fill (dscsv ); Dscsv. writexml (@ "C:/wang_odbc.xml "); }
Catch (exception ex)
{
Throw ex;
}
Finally
{
Objconn. Close ();
}
}
}
  // Source code for reading CSV files in oledb Mode Private void btnreadcsvbyoledb_click (Object sender, eventargs E)
{
This. opencvsfiledialog. Title = string. Format ("{0} Getting started successfully", "CSV cute ");
This. opencvsfiledialog. defaultext = "CSV ";
This. opencvsfiledialog. Filter = "CSV (カン) (*. CSV) | *. CSV ";
Dialogresult DR = This. opencvsfiledialog. showdialog (this );
If (DR = dialogresult. OK)
{
String strfilepathandname = opencvsfiledialog. filename;
String strconn = @ "provider = Microsoft. Jet. oledb.4.0; Data Source =" + system. Io. Path. getdirectoryname (strfilepathandname) + "; extended properties = text ;";
// Filename, for example: 1.csv
String strsql = @ "select * from" + system. Io. Path. getfilename (strfilepathandname );
Oledbconnection objconn = new oledbconnection (strconn );
Dataset dscsv = new dataset ();
Try
{
Oledbdataadapter oledbcsvdataadapter = new oledbdataadapter (strsql, objconn ); Oledbcsvdataadapter. Fill (dscsv ); Dscsv. writexml (@ "C:/wang_oledb.xml "); }
Catch (exception ex)
{
Throw ex;
}
Finally
{
Objconn. Close ();
}
}
}

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.