數據命令參數問題
有以下一段代碼:
SqlConnection conn = DBC.DatabaseConnection();
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from @tableName ";
cmd.Parameters.Add( "@tableName ", SqlDbType.NVarChar, 30).Value = DropDownList1.SelectedValue;
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
conn.Close();
為什麼運行時老是提示:
System.Data.SqlClient.SqlException: Must declare the variable '@tableName '.
而有明明有申明此變量啊
------解决方案--------------------表名不能是变量,
你一定要在传到SqlServer之前把这个表名明确化了才行.
------解决方案--------------------同意hbxtlhx(平民百姓) ..
or try..
cmd.CommandText = "select * from "+DropDownList1.SelectedValue.ToString();