OleDB选择多个CSV作为第一行作为字段名称在C# [英] OleDB selecting multiple CSVs with first row as field names in C#

查看:296
本文介绍了OleDB选择多个CSV作为第一行作为字段名称在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码看起来像这样,这是所有罚款和良好

So my code looks like this currently, which is all fine and good

String q = "SELECT * FROM "+"test.csv";
        try
        {
            OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\;Extended Properties=\"Text;HDR=No;FMT=Delimited\"");
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            OleDbCommand cd = new OleDbCommand(q, cn);
            cn.Open();
            da.SelectCommand = cd;
            ds.Clear();
            da.Fill(ds, "CSV");
            dataGridView1.DataSource = ds.Tables[0];
            cn.Close();
            }
        catch (Exception ex)
        {
            MessageBox.Show("There was an issue: " + ex.Message);
        }

但是,我想获取CSV中的第一行,作为字段名,所以我可以做类似

However, I would like to take the first row in my CSV and use that as the field names, so I can do something like

"select a.ID, a.SOMETHING_ELSE from test.csv a"
where the CSV looks something like
ID,SOMETHING_ELSE,DONT_WANT_THIS
1,blah,I don't want to see this

然而,从我看到的,关于网络上的所有示例没有什么比select *,我想我的datagrid显示标题作为字段名称选择,

Yet, from what I see, about all examples on the web show nothing other than select *, and I would like my datagrid to show the headers as the field names selected, instead of F1, F2....

推荐答案

更改连接字符串以包括HDR = Yes(而不是HDR = No):

Change your connection string to include HDR=Yes (instead of HDR=No):

OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\;Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"");

对于OleDB,连接字符串中的HDR属性指示第一行是否包含列名。

For OleDB, the HDR property in the connection string indicates whether or not the first row contains the column names.

这篇关于OleDB选择多个CSV作为第一行作为字段名称在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆