[英]Efficient Way to SQL Csv w/ > 255 Columns in C#?
I have the following OleDb method I really like because I can eventually pass SQL queries against a csv.我有以下我非常喜欢的 OleDb 方法,因为我最终可以通过针对 csv 的 SQL 查询。
The trouble is that OleDb can only read < 256 columns and I have more than this.问题是 OleDb 只能读取 < 256 列,而我拥有的不止这些。
What is an alternative to efficiently querying a csv table in C# with such a column size?以这样的列大小有效查询 C# 中的 csv 表的替代方法是什么?
I'd like to avoid bringing the entire csv into a datatable to later LINQ.我想避免将整个 csv 放入数据表中以供以后的 LINQ 使用。
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.Jet.OLEDB.4.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
{
Locale = CultureInfo.CurrentCulture
};
adapter.Fill(dataTable);
return dataTable;
}
}
Update: Tried a comment in the suggestion of the following script to use the ACE driver but the following OleDb connection string still only gets 255 columns.更新:尝试在以下脚本的建议中添加评论以使用 ACE 驱动程序,但以下 OleDb 连接字符串仍然只能获得 255 列。 Still looking for a way to accomplish this.
仍在寻找一种方法来实现这一点。
using (OleDbConnection connection = new OleDbConnection(
"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + pathOnly +"; Extended Properties =\"Text; HDR = Yes; FORMAT = Delimited\""))
How about something like this?:像这样的东西怎么样?:
static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
{
DataTable dataTable = null;
using (var inStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(inStream))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var fields = line.Split(',');
if (dataTable == null) // create datatable based on header row
{
dataTable = new DataTable();
foreach (var field in fields)
dataTable.Columns.Add(new DataColumn(field));
continue; // don't add header row to rows collection
}
dataTable.Rows.Add(fields);
}
}
return dataTable;
}
If your first row is not a header, you'd have to come up with a different approach to naming the columns.如果您的第一行不是 header,则您必须想出一种不同的方法来命名列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.