>此解決方案使用schema.ini檔案進行精確控制。 以下程式碼示範了此方法:
此方法有效地將CSV資料讀取到資料詞中,並根據需要將數字值作為文字字串儲存為文字字串,這要歸功於
<code class="language-csharp">using System.Data; using System.Data.OleDb; using System.Globalization; using System.IO; static DataTable LoadCsvIntoDataTable(string filePath, bool isFirstRowHeader) { string header = isFirstRowHeader ? "Yes" : "No"; string directory = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); string sqlQuery = $"SELECT * FROM [{fileName}]"; using (OleDbConnection connection = new OleDbConnection( $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={directory};Extended Properties=\"Text;HDR={header}\"")) using (OleDbCommand command = new OleDbCommand(sqlQuery, connection)) using (OleDbDataAdapter adapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.CurrentCulture; adapter.Fill(dataTable); return dataTable; } }</code>> file提供的配置。
>
以上是如何有效地將 CSV 檔案載入到 .NET 資料表中並將數值作為文字處理?的詳細內容。更多資訊請關注PHP中文網其他相關文章!