我試圖使用oledb命令讀取一個csv文件。以下是我正在使用。 此代碼執行順利。但我需要從csv文件中刪除第一行,並將第二行設置爲數據表的列標題。可能嗎?使用OLEDB命令讀取csv
static DataTable ImportCsvFileToDataTable(string filename, string fullPath)
{
FileInfo file = new FileInfo(fullPath);
using (OleDbConnection con =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
file.DirectoryName + "\";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';"))
{
using (OleDbCommand cmd = new OleDbCommand(string.Format
("SELECT * FROM [{0}]", file.Name), con))
{
con.Open();
// Using a DataTable to process the data
using (OleDbDataAdapter adp = new OleDbDataAdapter(cmd))
{
DataTable tbl = new DataTable(filename);
adp.Fill(tbl);
return tbl;
}
}
}
}