将查询结果链接到多个组合框

本文关键字:组合 查询 结果 链接 | 更新日期: 2023-09-27 18:32:34

我有几个从不同表中提取数据的依赖组合框(CB)。 它可以是所选值或查找值。 例如,我将在框中显示文本(名称),选择后,我需要查找相应的 int (ID) 并在查询中使用该值来填充下一个组合框。

我在引用命令/查询结果时遇到问题并收到数据类型错误。错误是:

SQL 直通表达式...使用 equals (=) 具有不同数据类型的组件。

注意:我的 oledb 接口不支持 ICommandWithParameters

// Get the ID (int) value - This works as I can convert the return value to a string and echo back to the screen
OleDbCommand tblRow2 = new OleDbCommand("select ID from Table1 where NM=  '"+ CB1.Text +"' ;" , conn3);
try
{
    conn3.Open();
    string r2 = Convert.ToString(tblRow2.ExecuteScalar());
    MessageBox.Show(r2);
    labelTableID.Text = "ID Code= " + r2;
    conn3.Close();
}
catch (Exception ex2)
{
    MessageBox.Show("Error " + ex2);
}
// Pass the ID (int) value and populate the next combo box CB2
// This doesn't work and throws the data type mismatch error
OleDbCommand tblRow3 = new OleDbCommand("select STATUS from Table2 where TABLE_ID = '"+ tblRow2 +"'; ",conn3);
OleDbDataReader rdRow3;
try
{
    conn3.Open();
    rdRow3 = tblRow3.ExecuteReader();
    while (rdRow3.Read())
    {
        CB2.Items.Add(rdRow3.GetString(0));
    }
    conn3.Close();
}

将查询结果链接到多个组合框

你用

撇号包围你的数字,所以它被视为字符串文字。

删除撇号。

var tblRow3 =
    new OleDbCommand("select STATUS from Table2 where TABLE_ID = " + tblRow2 + ";", conn3);

更好的是,参数化您的查询,这样您就不会遇到这样的错误。

这是未经测试的,但这样的事情应该有效。您也可以指定参数的名称,但我认为使用 OLEDB 时,名称并不像顺序那么重要。

using (var cmd = new OleDbCommand("select STATUS from Table2 where TABLE_ID = ?", conn3))
{
  cmd.Parameters.Add(new OleDbParameter {OleDbType = OleDbType.Integer, Value = tblRow2});
}

我已经回答了我的大部分问题,但我留下了一个逻辑错误,即我的 sql 查询在应该返回任何结果时没有返回任何结果。 同样,我对 C# 很陌生,所以我可能没有正确的命名法,但这是我对问题/解决方案的理解。 OleDbCommand 返回一个"对象",一个查询可以返回许多行和列,但我需要一个单一值在后续查询中进行比较......因此,ExecuteScalar() 可以解决问题。 一旦我有一个很好的值,我只需要将其转换为变量(字符串或 int)并在下一个查询中正确嵌入。 所以一切都"正常工作"(没有错误),我已经验证了每个查询的结果,所以我知道它们是正确的......但我没有从最后一个命令/查询( tblRow3a)得到任何结果。 有什么想法吗? 谢谢@

                        MessageBox.Show(CB1.Text);
        string conn3str = <connection string>;
        OleDbConnection conn3 = new OleDbConnection(conn3str);
        // Get the ID (int) value
        OleDbCommand tblRow2 = new OleDbCommand();
        tblRow2.Connection = conn3;
        tblRow2.CommandText = "select ID from Table1 where NM=  ' " + CB1.Text +" ' ;" ;
        conn3.Open();
        // Convert & Pass the command result (tblRow2) to string (info only) & int (for next query) 
        string r2 = Convert.ToString(tblRow2.ExecuteScalar());
        Label_1.Text = "ID Code= " + r2;
        int r2i = Convert.ToInt32(tblRow2.ExecuteScalar());
        // Select STATUS (string) where TABLE_ID (int) = ID (int)
        OleDbCommand tblRow3 = new OleDbCommand();
        tblRow3.Connection = conn3;
        tblRow3.CommandText = "select STATUS from Table2 where TABLE_ID = " + r2i;
        // Convert and Pass command result (tblRow3) to string for next query
        // Where CODE (string) = r3 (string) 
        string r3 = Convert.ToString(tblRow3.ExecuteScalar());
        Label_2.Text = "Current Status = " + r3;
        OleDbCommand tblRow3a = new OleDbCommand();
        tblRow3a.Connection = conn3;
        tblRow3a.CommandText = "select ORDER from Table3 where CODE =  ' " + r3 + " ' ;" ;
        string r3a = Convert.ToString(tblRow3a.ExecuteScalar());
        lblInvCd.Text = "Order = " + r3a;
        MessageBox.Show(r3a);
        // Result is empty even through this same query returns results in the database

我发现了问题,它实际上什么都没有,即空格。 错误: ' " + var + " ' 好: "+var+"' 所有查询现在都正常工作。