CSV to able (1)
Last Update:2018-12-05
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 (); } } }