>此解决方案使用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>
>
以上是如何有效地将CSV文件加载到.NET DATATABLE并将数字值作为文本处理?的详细内容。更多信息请关注PHP中文网其他相关文章!