关于字段名作为参数传递到sql的问题【急!!!在线等】
本人想用ASP.net的C#语言实现一个查询功能,把字段名放在一个dropdownlist中,把检索词放在tablebox中,然后通过sql语句来查找记录条数,及记录内容,代码如下:
string count = "0";
DataTable MyQueryTable = new System.Data.DataTable();
SqlConnection MyCon = new SqlConnection();
MyCon.ConnectionString = ConfigurationManager.ConnectionStrings["nana_testConnectionString"].ConnectionString;
MyCon.Open();
SqlCommand MyCm = new SqlCommand();
MyCm.Connection = MyCon;
MySql = "select count(*) as totalnum from 文件库 where " + this.ddl_ziduan1.SelectedValue.ToString().Trim() + "like '%" + this.tb_con1.Text + "%'";
MyCm.CommandText = MySql;
SqlDataReader MyDr = MyCm.ExecuteReader();
while (MyDr.Read())
{
count = MyDr["totalnum"].ToString().Trim();
}
MyCon.Close();
实现不了,插了相关资料说要用到declare @,那应该如何使用呢?请高手指教!谢谢!
急,在线等……
[解决办法]
"like '%" + this.tb_con1.Text + "%'"请在like前加空格