C#.NET 大量讀取csv檔
方法一(使用Odbc):
首先電腦要有odbc的驅動檔案。
cls.path是檔案所在的資料夾
using System.Data.Odbc;
OdbcConnection cn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" + cls.path);
OdbcCommand cmd = new OdbcCommand("select * from tmpCSV.csv", cn);
OdbcDataAdapter da = new OdbcDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds, "upload");
方法二(使用OleDb):
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + cls.path + "; Extended Properties='text;HDR=NO;FMT=Delimited';";
string.Format(conn, cls.path);
string CommandText = string.Format(@"SELECT * FROM tmp101.csv", conn);
OdbcDataAdapter da = new OdbcDataAdapter();
DataSet CSVDataSet = new DataSet();
OleDbConnection CSVConnection = new OleDbConnection(conn);
OleDbDataAdapter CSVAdapter = new OleDbDataAdapter(CommandText, CSVConnection);
CSVConnection.Open();
CSVAdapter.Fill(CSVDataSet, "upload");
CSVConnection.Close();
留言
張貼留言