读取两个TextBox的值实现sql中的查询命令解决方案

读取两个TextBox的值实现sql中的查询命令

string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=***";
                string sqlStr = "select * from 学生信息 where 年级>1 and 年级<8";
                using (SqlConnection con = new SqlConnection(conStr))
                {

                    DataSet ds = new DataSet();
                    con.Open();

                    SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con);

                    ada.Fill(ds);

                    GridView2.DataSource = ds.Tables[0];
                    GridView2.DataBind();

                    Label15.Text = "绑定成功";

运行上面代码得到结果正确如图
读取两个TextBox的值实现sql中的查询命令解决方案
但是当我想把条件改为从TextBox中读取的值时,就不能成功显示了。

string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=***";
                 string sqlStr = "select 学号,姓名,年级,名次,性别 from 学生信息 where '" + DropDownList1.Text + "'>'" + TextBox12.Text + "' and '" +  DropDownList1.Text  + "'<'"+TextBox13.Text+"'";
                 using (SqlConnection con = new SqlConnection(conStr))
                 {



                     DataSet ds = new DataSet();
                     con.Open();

                     SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con);

                     ada.Fill(ds);

                     GridView2.DataSource = ds.Tables[0];
                     GridView2.DataBind();

结果如图
读取两个TextBox的值实现sql中的查询命令解决方案
两个程序的差别仅在于string sqlStr这一行,请问要怎么写?
------解决思路----------------------
不要把
"年级"
用''
括起来!!!
------解决思路----------------------
where '" + DropDownList1.Text + "'

这里的DropDownList1.Text前后为何要加上单引号?这不就成了字符串与字符串比较么?
------解决思路----------------------
sql拼接的地方,字段名不能用单引号包含起来,值你是数字,也不能用单引号包含,简单的说把你sql的单引号都去掉
------解决思路----------------------
where '" + DropDownList1.Text + "'>'" + TextBox12.Text + "' and '" +  DropDownList1.Text  + "'<'"+TextBox13.Text+"'";
这样写的结果翻译成查询语句就是:
where '年级'>'1' and '年级'<'8‘;
跟你之前的查询语句对比:
"select * from 学生信息 where 年级>1 and 年级<8";
列表不需要分号,查询的值如果不是字符型,也不需要分号。