首页 > 解决方案 > 使用like在查询中获取多个值

问题描述

我有这个查询。

select * from Table1 where ID like @id

这被称为如下:

cmd.Parameters.AddWithValue("@id", ds.Tables[0].Rows[j].ItemArray[6].ToString());

这是我的全部代码。

con = new SqlConnect();
con.SqlQuery("Select * from EmployeeDeduct where EmployeeeID = @empID");
con.cmd.Parameters.AddWithValue("@empID", lblEmpID.Text);
sda = new SqlDataAdapter(con.cmd);
ds = new DataSet();
sda.Fill(ds);
con.cmd.Connection.Close();
if (ds.Tables[0].Rows.Count > 0)
{
    for (int g = 0; g < ds.Tables[0].Rows.Count; g++)
    {
        con = new SqlConnect();
        con.SqlQuery("Select * from DeductionInfo");
        sda = new SqlDataAdapter(con.cmd);
        DataSet ds2 = new DataSet();
        sda.Fill(ds2);
        con.cmd.Connection.Close();
        if (ds2.Tables[0].Rows.Count > 0)
        {
            for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
            {

                if (ds.Tables[0].Rows[g].ItemArray[0].ToString() == ds2.Tables[0].Rows[j].ItemArray[6].ToString())
                {
                    //LvDeductions.Items[j].Checked = true;
                    con = new SqlConnect();
                    con.SqlQuery("Select * from DeductionInfo where DeductID like @id");
                    con.cmd.Parameters.AddWithValue("@id", ds2.Tables[0].Rows[j].ItemArray[6].ToString());
                    ListViewDed();
                }
            }
        }
    }
}

我只得到一排而不是三排。请问有什么想法吗?请看图片

在此处输入图像描述

这是到目前为止的工作代码

con = new SqlConnect();
con.SqlQuery("Select * from DeductionInfo");
ListViewDed();
con = new SqlConnect();
con.SqlQuery("Select * from EmployeeDeduct where EmployeeeID = @empID");
con.cmd.Parameters.AddWithValue("@empID", lblEmpID.Text);
sda = new SqlDataAdapter(con.cmd);
ds = new DataSet();
sda.Fill(ds);
con.cmd.Connection.Close();
if (ds.Tables[0].Rows.Count > 0)
{
    for (int g = 0; g < ds.Tables[0].Rows.Count; g++)
    {
        con = new SqlConnect();
        con.SqlQuery("Select * from DeductionInfo");
        sda = new SqlDataAdapter(con.cmd);
        DataSet ds2 = new DataSet();
        sda.Fill(ds2);
        con.cmd.Connection.Close();
        if (ds2.Tables[0].Rows.Count > 0)
        {
            for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
            {

                if (ds.Tables[0].Rows[g].ItemArray[0].ToString() == ds2.Tables[0].Rows[j].ItemArray[6].ToString())
                {
                    LvDeductions.Items[j].Checked = true;
                    //con = new SqlConnect();
                    //con.SqlQuery("Select * from DeductionInfo where DeductID like @id");
                    //con.cmd.Parameters.AddWithValue("@id", ds2.Tables[0].Rows[j].ItemArray[6].ToString());
                    //ListViewDed();
                }
            }
        }
    }
}

请在此处查看图片

在此处输入图像描述

我想更改代码:

// LvDeductions.Items[j].Checked = true;

使用这个

// con = new SqlConnect();
// con.SqlQuery("Select * from DeductionInfo where DeductID like @id");
// con.cmd.Parameters.AddWithValue("@id", ds2.Tables[0].Rows[j].ItemArray[6].ToString());
// ListViewDed();"

所以我可以删除复选框。

标签: c#sqlsql-server

解决方案


在您的代码中

cmd.Parameters.AddWithValue("@id", ds.Tables[0].Rows[j].ItemArray[6].ToString());

代码ds.Tables[0].Rows[j].ItemArray[6].ToString()必须包含 SQL Like Wildcards%_


推荐阅读