我有这个代码,当我搜索特定内容时,它可以正常工作。如果我只知道Order_ID的一部分,我怎么能改变它以便它可以工作。我尝试了一切,我想我只是没有以正确的方式做到这一点。
SqlConnection con = new SqlConnection(@"Data Source=ANNA\MSSQLSERVER2012;Initial Catalog=Wine house;Integrated Security=True;");
con.Open();
string strSQL1 = "";
if (textBox1.Text.Length > 0)
{
if (strSQL1.Length > 0)
{
strSQL1 = strSQL1 + " AND ";
}
strSQL1 = strSQL1 + "Order_ID=" + textBox1.Text;
}
if (textBox2.Text.Length > 0)
{
if (strSQL1.Length > 0)
{
strSQL1 = strSQL1 + " AND ";
}
strSQL1 = strSQL1 + "Client_ID=" + textBox2.Text;
}
if (textBox3.Text.Length > 0)
{
if (strSQL1.Length > 0)
{
strSQL1 = strSQL1 + " AND ";
}
strSQL1 = strSQL1 + "Amount_due=" + textBox3.Text;
}
if (textBox4.Text.Length > 0)
{
if (strSQL1.Length > 0)
{
strSQL1 = strSQL1 + " AND ";
}
strSQL1 = strSQL1 + "Employee_ID=" + textBox4.Text;
}
if (textBox5.Text.Length > 0)
{
if (strSQL1.Length > 0)
{
strSQL1 = strSQL1 + " AND ";
}
strSQL1 = strSQL1 + "DeliveryDate='" + textBox5.Text;
}
if (textBox6.Text.Length > 0)
{
if (strSQL1.Length > 0)
{
strSQL1 = strSQL1 + " AND ";
}
strSQL1 = strSQL1 + "Delivered='" + textBox6.Text + "'";
}
if (strSQL1.Length > 0)
{
strSQL1 = @"SELECT Order_ID, Client_ID, Amount_due, Employee_ID, CONVERT(nvarchar, DeliveryDate, 104) AS DeliveryDate, Delivered FROM Orders WHERE" + strSQL1;
}
else
{
strSQL1 = @"SELECT Order_ID, Client_ID, Amount_due, Employee_ID, CONVERT(nvarchar, DeliveryDate, 104) AS DeliveryDate, Delivered FROM Orders";
}
SqlCommand cmd = new SqlCommand(strSQL1, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
dataGridView1.DataSource = new BindingSource(table, null);
con.Close();
答案 0 :(得分:0)
您可以使用 like 运算符,请参阅:
http://www.w3schools.com/sql/sql_like.asp
类似的东西:
WHERE Order_ID LIKE '%THE_ID_FRACTION_YOU_HAVE%'
“%”是一个通配符,即使您知道的部分不是ID的开头或结尾,也可以搜索。
答案 1 :(得分:0)
使用您的示例,您可以执行以下操作:
strSQL1 = strSQL1 + "Order_ID LIKE '%" + textBox1.Text + "%'";