请解决错误必须声明标量变量“@ Role”。 [英] Please solve the error Must declare the scalar variable "@Role".

查看:117
本文介绍了请解决错误必须声明标量变量“@ Role”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

con.Open();





使用(SqlCommand comm = new SqlCommand(从Role1中选择[添加],其中Role_Name = @ Role和Pages = @ Pages,con))

{// 2.定义命令对象中使用的参数

SqlParameter para = new SqlParameter();

para.ParameterName =@ Role; para.Value = Session [Role];

para.ParameterName =@ Pages; para.Value =Checkin; // 3.向命令对象添加新参数

comm.Parameters.Add(para); // //读入SELECT结果//

SqlDataReader reade = comm.ExecuteReader();

while(reade.Read())

{Session [Add] = Convert.ToString(reade [Add]); }

//按钮添加=(按钮)PreviousPage.FindControl(Button2);

if(Session [Add]。ToString()==Y )

{

Button2.Visible = true;

}

else

{

Button2.Visible = false;

}

con.Close();

}

con.Open();


using (SqlCommand comm = new SqlCommand("select [Add] from Role1 where Role_Name=@Role and Pages=@Pages ", con))
{ // 2. define parameters used in command object
SqlParameter para = new SqlParameter();
para.ParameterName = "@Role"; para.Value = Session["Role"];
para.ParameterName = "@Pages"; para.Value = "Checkin";// 3. add new parameter to command object
comm.Parameters.Add(para); // // Read in the SELECT results. //
SqlDataReader reade = comm.ExecuteReader();
while (reade.Read())
{ Session["Add"] = Convert.ToString(reade["Add"]); }
// Button Add = (Button)PreviousPage.FindControl("Button2");
if (Session["Add"].ToString() == "Y")
{
Button2.Visible = true;
}
else
{
Button2.Visible = false;
}
con.Close();
}

推荐答案

您正在创建一个单独的SqlParameter变量,并尝试使用它来定义两个参数。

这是行不通的。

你的代码应该更好看:

You are creating one single SqlParameter variable and try to use it to define two parameters.
That cannot work.
Your code should better look like:
con.Open();
using (SqlCommand comm = new SqlCommand("select [Add] from Role1 where Role_Name=@Role and Pages=@Pages", con))
{
  // 2. define parameters used in command object
  comm.Parameters.AddWithValue("@Role", Session["Role"]);
  comm.Parameters.AddWithValue("@Pages", "Checkin");
  SqlDataReader reade = comm.ExecuteReader();
  while (reade.Read())
  {
    Session["Add"] = Convert.ToString(reade["Add"]);
  }
  // Button Add = (Button)PreviousPage.FindControl("Button2");
  Button2.Visible = (Session["Add"].Equals("Y", StringComparison.InvariantCultureIgnoreCase));
}
con.Close();





希望这会有所帮助。祝你好运。



Hope this helps. Good luck.


正确编写代码。你没有传递 @Role 的传递值。

Do write your code properly. You didn't pass the pass value for @Role.
SqlParameter para = null;

//Pass @Role parameter
para = new SqlParameter();
para.ParameterName = "@Role"; 
para.Value = Session["Role"];
comm.Parameters.Add(para);

//Pass @Pages parameter
para = new SqlParameter();
para.ParameterName = "@Pages"; 
para.Value = "Checkin";
comm.Parameters.Add(para);


这篇关于请解决错误必须声明标量变量“@ Role”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆