问题是什么 ? '='附近的语法不正确。必须声明标量变量“@ Password”。 [英] What is the problem ? Incorrect syntax near '='. Must declare the scalar variable "@Password".

查看:68
本文介绍了问题是什么 ? '='附近的语法不正确。必须声明标量变量“@ Password”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnRegister_Click(object sender, EventArgs e)
{
    string cs = @"Data Source=.\SQLEXPRESS; 
                AttachDbFilename=|DataDirectory|\Database.mdf; 
                Integrated Security=True; 
                User Instance=True";

    string sql = "INSERT INTO Member (Username, UserPassword, Name, IC, Gender, Address, Email) VALUES (@Username, @Password, @Name, @IC, @Gender, @Address, @Email)";

    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand(sql, con);

    cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
    cmd.Parameters.AddWithValue(Encrypt("@Password"), txtPassword.Text);
    cmd.Parameters.AddWithValue("@Name", txtName.Text);
    cmd.Parameters.AddWithValue("@IC", txtIC.Text);
    cmd.Parameters.AddWithValue("@Gender", ddlGender.Text);
    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
    cmd.Parameters.AddWithValue("@Email", txtEmail.Text);

    con.Open();        
    int number =  cmd.ExecuteNonQuery();//this line has a problem
    con.Close();

    if (number >= 1)
    {
        lblResult.Text = number + " Success Insert";
    }
    else
    {
        lblResult.Text = "Please try again";
    }
}

private string Encrypt(string clearText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            clearText = Convert.ToBase64String(ms.ToArray());
        }
    }
    return clearText;
}

推荐答案

这个:



This:

cmd.Parameters.AddWithValue(Encrypt("@Password"), txtPassword.Text);





应该是:





Should probably be:

cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text));





否则使用原始代码,您告诉它加密参数名称而不是文本中的文本文本框。加密的参数名称与定义的名称不匹配,命令无法再找到名为@Password的参数。



Otherwise with your original code, you are telling it to encrypt the parameter name not the text in the textbox. The encrypted parameter name does not match the defined name and the command can't find the parameter named @Password anymore.


cmd.Parameters.AddWithValue(Encrypt("@Password"), txtPassword.Text);



在这里你应该加密值而不是参数。



所以,把它改成......


Here you should Encrypt the Value not the Parameter.

So, change it to...

cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text));


您正在加密参数名称而不是密码值,



试试这个



You are encrypting the parameter name instead of the password value,

Try this

cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text));


这篇关于问题是什么 ? '='附近的语法不正确。必须声明标量变量“@ Password”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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