string sql = @"select * from Publish where Email=@email order by PublicTime desc";
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@email", SqlDbType.NVarChar, 50),
};
param[0].Value = Session["strnickemail"];
为什么运行的时候它老是提示必须声明标量变量 "@email"??
缓存里面的Session["strnickemail"]="123@1.com"
在跟数据库连接的时候:
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddRange(param);
conn.Open();
为什么会出现这个问题吖。。。。求解吖。。。。求解吖。。
------解决方案--------------------------------------------------------
没有把command赋给Adpater
------解决方案--------------------------------------------------------
- C# code
protected void GetListByUserName(){ string sql = "select * from Publish where Email=@email order by PublicTime desc"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@email", SqlDbType.NVarChar, 50) }; param[0].Value = Session["strnickemail"]; try { using (SqlConnection conn = new SqlConnection(Common.ConnString)) { SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddRange(param); conn.Open(); SqlDataAdapter dr = new SqlDataAdapter(); dr.SelectCommand=comm; //更正一下,这里是你SqlCommand的对象 DataSet ds = new DataSet(); dr.Fill(ds); DataList1.DataSource = ds; DataList1.DataBind(); } } catch (Exception ex) { //throw ex; lblError.Text = "显示失败,请稍候再重试" + ex.Message; lblError.Visible = true; } }}