string name = textBox1.Text.Trim();
string teacher = textBox2.Text.Trim();
String sql4 = "Select * from course where 1=1";
if (!String.IsNullOrEmpty(name))
{
sql4 += " and Name=" + name;
}
if (!String.IsNullOrEmpty(teacher))
{
sql4 += "and Teacher like '%" + teacher + "%'";
}
sql4 += " order by Name";
string connString = @"Data Source=(local); Initial Catalog=学生成绩管理系统; Integrated Security=true";
SqlConnection conn = new SqlConnection(connString);
da = new SqlDataAdapter(sql4, conn);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
1、原因
在程序中值没有给他加引号,所以当成int型处理了。
2、解决方法
加上引号就行了,而且sql的单引号双引号是一样的。
希望对你有帮助~~~