c# - 使用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();"
所以我可以删除复选框。
解决方案
在您的代码中
cmd.Parameters.AddWithValue("@id", ds.Tables[0].Rows[j].ItemArray[6].ToString());
代码ds.Tables[0].Rows[j].ItemArray[6].ToString()
必须包含 SQL Like Wildcards%
或_
推荐阅读
- javascript - Innerhtml 作为 php echo 包括
- php - 如何向 Oracle 用户添加授权以在 BLOB 列中插入数据
- sql - 如何在 SQL Server 中编写以下条件
- git - 克隆远程存储库时遇到错误:Git failed with a fatal error
- javascript - Ant Design 添加纯文本菜单项
- sql - 如何向 Azure SQL 数据库中的用户授予数据库范围凭据
- python-3.x - 在设计神经网络模型时无法将字符串转换为浮点数
- macos - 如何将新文件夹图标设置为 Volumes 文件夹
- phaser-framework - Phaser 3 - 未解析的方法图形
- html - 创建一个带有文本的边框