0

我尝试使用 sqlparameters 将数据插入数据库,但我无法做到这一点..请帮助我..

a = "insert into Matsamp values(Login_Name,Password,Confirm_Password) values(@LName,@Pswd,@CPassword)";
        SqlCommand cm = new SqlCommand(a, con1);
        SqlParameter Uparm;
        Uparm = new SqlParameter("@LName", SqlDbType.VarChar, 25);
        Uparm.Value = TextBox1.Text;
        cm.Parameters.Add(Uparm);

        string original;
        original = TextBox2.Text.Trim();
        int h = original.GetHashCode();
        string withHash = original;
        b1 = Encoding.BigEndianUnicode.GetBytes(withHash);
        encrypted = Convert.ToBase64String(b1);
        SqlParameter Pparam;
        Pparam = new SqlParameter("@Pswd", SqlDbType.VarChar, 300);
        Pparam.Value = Convert.ToString(encrypted);
        cm.Parameters.Add(Pparam);

        SqlParameter CPparm;
        CPparm = new SqlParameter("@CPassword", SqlDbType.VarChar, 300);
        CPparm.Value = TextBox3.Text;
        cm.Parameters.Add(CPparm);
        cm.ExecuteNonQuery();
        Response.Write("<Script>alert('inserted')</script>");
    }
        con1.Close();
4

1 回答 1

0

您的查询是错误的:可能应该是

a = "insert into Matsamp(Login_Name,Password,Confirm_Password) 
     values(@LName,@Pswd,@CPassword)";

你可以使用

cm.Parameters.AddWithValue("@LName", TextBox1.Text);
cm.Parameters.AddWithValue("@Pswd", ...);
cm.Parameters.AddWithValue("@CPassword", ...);

使其更短。

于 2012-05-21T06:07:48.977 回答