从excel列接收数据,并在组合框中的值更改时在texbox中显示

时间:2017-08-21 13:28:28

标签: c# .net oledb

enter image description here enter image description here enter image description here enter image description here从excel列接收数据,并在组合框中的值更改时更改它们。

以下是代码:

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    con.Open();         
    str = "select * from [Sac Haddehanesi Kalite Kontrol] where [Bobin ID]='" + comboBox1.Text.Trim() + "'";
    com = new OleDbCommand(str, con);
    OleDbDataReader reader = com.ExecuteReader();
    if (reader.Read())
    {
        comboBox1.Text = reader["Bobin ID"].ToString();
        textBox1.Text = reader["Slab ID"].ToString();
        textBox2.Text = reader["Döküm Kalitesi"].ToString();
        textBox3.Text = reader["Sipariş Çelik Kalitesi"].ToString();
        textBox4.Text = reader["Standart Kalite Adı"].ToString();
        textBox5.Text = reader["Bobin Planlanan Kalınlık (mm)"].ToString();
    }
    con.Close();
    reader.Close();
}

任何帮助都将受到高度赞赏......

谢谢你

1 个答案:

答案 0 :(得分:0)

Excel表格名称以$结尾,当工作表的名称中包含空格时,您必须将其用单引号括起来。因此你的SQL应该读......

 Select * From ['Sac Haddehanesi Kalite Kontrol$']

你应该正确地对你的查询进行参数设置......

Select * From ['Sac Haddehanesi Kalite Kontrol$'] Where [Bobin ID] = ?

OLEDB参数是位置而不是命名(如在SQL / Server中)。即按照它们在查询中出现的顺序指定它们。

因此你的代码变成......

OleDbCommand com = new OleDbCommand("Select * From ['Sac Haddehanesi Kalite Kontrol$'] Where [Bobin ID] = ?", conn);
com.Parameters.Add("?", OleDbType.VarChar).Value = "abc";
OleDbDataReader reader = com.ExecuteReader();

您可以通过...

查看工作簿中的表格
DataTable TablesList = conn.GetSchema("Tables");

foreach (DataRow TableRow in TablesList.Rows)
{
    if (TableRow["TABLE_NAME"].ToString().EndsWith("$") | TableRow["TABLE_NAME"].ToString().EndsWith("$'"))
    {
        // Handle the Excel Sheet
    }
}

顺便提一下,您没有在代码中向我们展示您的连接字符串。这是我用于上面的那个...

conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Test.xlsx;Extended properties='Excel 12.0;HDR=Yes;IMEX=1'";