必须声明标量变量 "@name"。
本帖最后由 as86238582 于 2012-11-29 00:34:15 编辑 我定义了一个sql类..
这是sql类中的登录方法
public bool checklogin(string name,string pwd)
{
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open();
// 创建命令(SqlCommand)对象(cmd)
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn; // 指示命令发给哪个数据库
cmd.CommandType = CommandType.Text; // 指示是什么类型的命令
cmd.CommandText = "SELECT [username],[password] FROM [user] WHERE [username]=@name and [password]=@pwd";
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
return true;
}
else {
return false;
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string name = this.txtName.Text; //得到登陆帐号
string pwd = this.txtpwd.Text; //得到登陆密码
sql db = new sql();
bool cklogin=db.checklogin(name,pwd);
if(cklogin=true){
Response.Write("<script>alert('恭喜你登录成功')</script>");
}
else{
Response.Write("<script>alert('恭喜你登录失败')</script>");
}
}
-----------------------------
求解
[最优解释]
public bool checklogin(string name,string pwd)
{
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open();
// 创建命令(SqlCommand)对象(cmd)
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn; // 指示命令发给哪个数据库
cmd.CommandType = CommandType.Text; // 指示是什么类型的命令
cmd.CommandText = "SELECT [username],[password] FROM [user] WHERE [username]=@name and [password]=@pwd";
//给参数赋值
cmd.Paramters.AddWithValue("@name", name);
cmd.Paramters.AddWithValue("@pwd", pwd);
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
return true;
}
else {
return false;
}
}