![](/img/trans.png)
[英]Insert and Registration data not show in database table visual studio 2019
[英]Insert login and registration data not show in SQL Server database table Visual Studio 2019
我正在使用 Visual Studio 2019 制作一個工具。當我嘗試單擊注冊按鈕時沒有出現錯誤。 但是當我打開數據庫並刷新表時,data.table中沒有數據。
非常感謝對此問題的任何支持。 謝謝你。
private void submitBtn_Click(object sender, EventArgs e)
{
//connection string
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\ImageSteganography_2\ImageSteganography\Database1.mdf;Integrated Security=True");
// Connection open here
conn.Open();
SqlCommand cmd = new SqlCommand();
if (repeatPassBox.Text != string.Empty || passBox.Text != string.Empty || contactBox.Text != string.Empty || emailBox.Text != string.Empty || userBox.Text != string.Empty)
{
//Email Address
if (validate_email.IsMatch(emailBox.Text) != true)
{
MessageBox.Show("Invalid Email Address!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
emailBox.Focus();
}
// Contacts
else if (validate_contact.IsMatch(contactBox.Text) != true)
{
MessageBox.Show("Contact accept numbers only (10 digit).", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
contactBox.Focus();
}
// Password
else if (validate_password.IsMatch(passBox.Text) != true)
{
MessageBox.Show("Password must be atleast 8 to 15 characters. It should contain at least one Upper case and numbers.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
passBox.Focus();
}
// Repeat Password
else if (passBox.Text != repeatPassBox.Text)
{
MessageBox.Show("Please enter both password same ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
cmd = new SqlCommand("select * from Login where username='" + userBox.Text + "'", conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
dr.Close();
MessageBox.Show("Username already exists - please try another ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
dr.Close();
cmd = new SqlCommand("insert into Registration values (@username, @email, @contact, @password, @repeatPassword)", conn);
cmd.Parameters.AddWithValue("username", userBox.Text);
cmd.Parameters.AddWithValue("email", emailBox.Text);
cmd.Parameters.AddWithValue("contact", contactBox.Text);
cmd.Parameters.AddWithValue("password", passBox.Text);
cmd.Parameters.AddWithValue("repeatPassword", repeatPassBox.Text);
cmd = new SqlCommand("insert into Login values(@username,@password)", conn);
cmd.Parameters.AddWithValue("username", userBox.Text);
cmd.Parameters.AddWithValue("password", passBox.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Register successful. Please login now.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
LOGIN f1 = new LOGIN();
f1.ShowDialog();
}
conn.Close();
}
}
else
{
MessageBox.Show("All input fields cannot be blank.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
這將修復原始版本中的許多小問題,有些不是那么小,並且作為獎勵應該運行得更快。 也就是說,它還有很多代碼直接輸入到回復 window 中,因此它可能還有一個或五個自己的錯誤有待解決:
private (string, Control) CheckRegistrationData()
{
//Email Address
if (emailBox.Text == "" || validate_email.IsMatch(emailBox.Text) != true)
{
return ("Invalid Email Address!", emailBox);
}
//Contacts
if (contactBox.Text == "" || validate_contact.IsMatch(contactBox.Text) != true)
{
return ("Contact accept numbers only (10 digit).", contactBox);
}
// Password
if (passBox.Text == "" || validate_password.IsMatch(passBox.Text) != true)
{
return ("Password must be atleast 8 to 15 characters. It should contain at least one Upper case and numbers.");
}
//Repeat Password
if (repeatPassBox.Text == "" || passBox.Text != repeatPassBox.Text)
{
return ("Please enter both password same ", repeatPassBox);
}
return ("", null);
}
private void submitBtn_Click(object sender, EventArgs e)
{
var validationResult = CheckRegistrationData();
if (!string.IsNullOrEmpty(validationResult.Item1))
{
MessageBox.Show(validationResult.Item1, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (validationResult.Item2 is Control)
{
valiadtionResult.Item2.Focus();
}
return;
}
// VALIDATION COMPLETE
string SQL = @"
IF EXISTS(SELECT 1 FROM Login WHERE Username= @username )
BEGIN
RAISERROR('Username Already exist',16,1);
END
ELSE
BEGIN
INSERT INTO Registration (Username, Email, Contact, PasswordHash)
VALUES (@username,@email,@contact,@passwordHash);
INSERT INTO Login (UserName, PasswordHash) VALUES (@username,@passwordHash);
END
";
using (var conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\ImageSteganography_2\ImageSteganography\Database1.mdf; Integrated Security = True"))
using (var cmd = new SqlCommand(SQL, conn))
{ //Much better to replace AddWithValue() with Add() and provide explicit type information about the database columns.
cmd.Parameters.AddWithValue("@username", userBox.Text);
cmd.Parameters.AddWithValue("@email", emailBox.Text);
cmd.Parameters.AddWithValue("@contact", contactBox.Text);
cmd.Parameters.AddWithValue("@passwordHash", BCrypt.Net.BCrypt.HashPassword(passBox.Text)); //Based on BCrypt.Net Nuget package
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(SqlException ex) when (ex.Errors[0].Class == 16)
{
MessageBox.Show("Username Already exist please try another ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
return;
}
catch(Exception ex)
{
MessageBox.Show($"Error trying to create registration: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
return;
}
}
MessageBox.Show("Register successful. Please login now.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
var f1 = new LOGIN();
f1.ShowDialog();
}
您錯誤地設置了命令參數:嘗試這樣的事情:
cmd.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = userBox.Text;
此外,使用兩個 Executenonquerys,一個用於注冊,另一個用於其他表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.