简体   繁体   中英

Insert String value into Database C#

I've got my passwords to be hashed in my ASP.NET Webforms.

How do I then enter the hashed password into the database via a string?

SqlConnection dbCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConMotorstore"].ConnectionString);

dbCon.Open();

SqlCommand cmd = new SqlCommand("INSERT INTO [user] VALUES (@firstName, @surname, @email, @username, @passwordHash)", dbCon);

cmd.Parameters.AddWithValue("firstName", firstNameTxt.Text);
cmd.Parameters.AddWithValue("surname", surnameTxt.Text);
cmd.Parameters.AddWithValue("email", emailTxt.Text);
cmd.Parameters.AddWithValue("username", usernameTxt.Text);

string passwordHash = BCrypt.Net.BCrypt.HashPassword(passwordTxt.Text);
cmd.Parameters.ToString("passwordHash");

cmd.ExecuteNonQuery();

I knew I couldn't use .AddWithValue and thought of .ToString may have been the one to use.

I am new to C#.

Thanks.

Does this work?

   SqlConnection dbCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConMotorstore"].ConnectionString);
{
    dbCon.Open();
    SqlCommand cmd = new SqlCommand("INSERT INTO [user] VALUES (@firstName,@surname,@email,@username,@passwordHash)", dbCon);
    cmd.Parameters.AddWithValue("firstName", firstNameTxt.Text);
    cmd.Parameters.AddWithValue("surname", surnameTxt.Text);
    cmd.Parameters.AddWithValue("email", emailTxt.Text);
    cmd.Parameters.AddWithValue("username", usernameTxt.Text);
    string passwordHash = BCrypt.Net.BCrypt.HashPassword(passwordTxt.Text);
    cmd.Parameters.AddWithValue("passwordHash", passwordHash);


    cmd.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM