我想在 c# 语言中使用 ado.net 将 .csv 文件与 Windows 窗体应用程序连接起来。这样我就可以从 .csv 文件中检索数据并使用 sql 查询将其存储在我的 Windows 窗体应用程序中。 请提供完整的连接字符串。 谢谢
最佳答案
没有什么比“csv 的连接字符串”更好的了。如果你想查询 CSV,你可以使用 LINQ to CSV - 参见 some examples .然后您可以将 LINQ 结果绑定(bind)到例如DataGridView 的 DataSource
属性。
CsvFileDescription inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
CsvContext cc = new CsvContext();
IEnumerable<Product> products =
cc.Read<Product>("products.csv", inputFileDescription);
// Data is now available via variable products.
var productsByName =
from p in products
orderby p.Name
select new { p.Name, p.LaunchDate, p.Price, p.Description };
grid.DataSource = productsByName.ToList();
关于c# - 使用 ado.net 和 c# 连接 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16000207/