接收页面: string ss = Session["kind"].ToString(); string sss = Session["content"].ToString();
sda.SelectCommand = new SqlCommand("select * from 影视档案表 where 地域 = '" + sss + "'"); 能够正确查出信息, 但sda.SelectCommand = new SqlCommand("select * from 影视档案表 where '" + ss + "'= '" + sss + "'");
全部代码:做的一个bind(),为GridView翻页使。 private void Bind() { SqlConnection con = DB.createConnection(); SqlDataAdapter sda = new SqlDataAdapter(); string ss = Session["kind"].ToString(); string sss = Session["content"].ToString(); sda.SelectCommand = new SqlCommand("select * from '" + ss + "' where 地域 = '" + sss + "'"); DataSet ds = new DataSet(); sda.SelectCommand.Connection = con; sda.Fill(ds, "影视档案表"); GridView1.DataKeyNames = new string[] { "电影中文名" }; this.GridView1.DataSource = ds.Tables["影视档案表"]; this.GridView1.DataBind(); }
------解决方案-------------------- where后面是字段名=值。。 这样写就行: sda.SelectCommand = new SqlCommand("select * from " + ss + " where 地域 = '" + sss + "'");
------解决方案--------------------
C# code
sda.SelectCommand = new SqlCommand("select * from 影视档案表 where " + ss + "= '" + sss + "'");
------解决方案-------------------- sda.SelectCommand = new SqlCommand("select * from 影视档案表 where '" + ss + "'= '" + sss + "'"); -----> sda.SelectCommand = new SqlCommand("select * from 影视档案表 where " + ss + " = '" + sss + "'");
------解决方案-------------------- 字段名是不需要加單引號的,所以去掉單引號及行了 sda.SelectCommand = new SqlCommand("select * from 影视档案表 where " + ss + "= '" + sss + "'");