0
我正在使用下面的OleDbCommand從文件方法多次讀取。我遇到的問題是,大多數時候它將數據類型作爲字符串返回,但偶爾(取決於正在讀取的數據)它返回的int類型。有沒有辦法請求所有列返回爲字符串,而不必在加載後轉換所有內容?定義OleDbCommand返回類型
static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
{
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = @"SELECT * FROM [" + fileName + "]";
using (OleDbConnection connection = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
return dataTable;
}
}
你有沒有聽說過''LINQ''的?這是LINQ中你可以更輕鬆地完成的事情。 – abhi
@abhi來自textfile的數據表?會喜歡上面給出的例子。 – windowskm
您需要一個模式文件。請參閱以下發布:https://stackoverflow.com/questions/12259562/how-can-i-create-schema-ini-file-i-need-to-export-my-csv-file-to-datagridview。 – jdweng