C#数据库编程时出现列名无效问题

各位,我在使用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];

这是我程序的界面

img


在使用教师姓名查询时可以成功查询

img


但是使用课程名称就无法查询,例如使用C语言查询时显示列名“C语言”无效

img


这是我表的内容

img

我所疑惑的是,为什么会显示列名“C语言”无效?,并且该如何解决,

1、原因

在程序中值没有给他加引号,所以当成int型处理了。

2、解决方法

加上引号就行了,而且sql的单引号双引号是一样的。

希望对你有帮助~~~