【发布时间】:2017-08-05 16:46:59
【问题描述】:
我收到运行时错误“必须声明标量变量@name”
public partial class UserMainPage : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["FindConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
label1.Text = Session["name"].ToString();
string name = label1.Text;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select balanceamount from wallet where username LIKE '%'+@name+'%'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
reader.Read();
walletbalance.Text = reader["balanceamount"].ToString();
reader.Close();
con.Close();
}
}
【问题讨论】:
-
在
ExecuteReader之前添加SqlParameter查询:com.Parameters.AddWithValue("@name", name)。 -
好吧,您没有为 SQL 查询声明 @name,因此错误消息是正确的。
-
这是代码转储,不是问题。
标签: c# asp.net sql-server