各位,我在使用C#数据库编程时,想查询数据库中符合条件的一行并将其显示在datagridview中,但遇到报错显示列名无效
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];
这是我程序的界面
在使用教师姓名查询时可以成功查询
但是使用课程名称就无法查询,例如使用C语言查询时显示列名“C语言”无效
这是我表的内容