C# 使用c.net中不工作的and运算符搜索查询
这是我为搜索而编写的代码…如果两个字段都只填充了,那么它应该返回结果…但它在语法上给出了错误…有人能帮我吗???问题:您使用了&&来组合这两个条件。 解决方案:您应该使用和,而不是&& 试试这个:C# 使用c.net中不工作的and运算符搜索查询,c#,C#,这是我为搜索而编写的代码…如果两个字段都只填充了,那么它应该返回结果…但它在语法上给出了错误…有人能帮我吗???问题:您使用了&&来组合这两个条件。 解决方案:您应该使用和,而不是&& 试试这个: OleDbConnection con = new OleDbConnection(constr); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con;
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select customer_id,customer_name,customer_mobile1,customer_address from tb_customer where ( (customer_mobile1 = ? ) && (customer_name = ? ))";
cmd.Parameters.AddWithValue("customer_mobile1", txt_no.Text);
cmd.Parameters.AddWithValue("customer_name", txt_name.Text);
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < 4; j++)
{
dataGridView2.Rows.Add(new DataGridViewRow());
dataGridView2.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
}
}
con.Close();
命令文本将在数据库中运行。因此它是SQL而不是C。在SQL中是而不是&另外,如果您还没有这样做,请确保清除文本框中的值
cmd.CommandText = "select customer_id,customer_name,customer_mobile1,customer_address from tb_customer where ( (customer_mobile1 = ? ) AND (customer_name = ? ))";
这可能会导致sql注入攻击。因此,请确保过滤并清除任何用户输入的数据。处处总是
cmd.Parameters.AddWithValue("customer_mobile1", txt_no.Text);
cmd.Parameters.AddWithValue("customer_name", txt_name.Text);