[英]Issue with SQL Server database in Registration form Visual Studio
This is my code: 这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Dido\Documents\DataCars.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("insert into Login values('" + textBox1.Text + "','" + textBox2.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
if (!String.IsNullOrEmpty(textBox1.Text) || !String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("Your registration was successfull!");
Login frm1 = new Login();
Global.GlobalVar = textBox1.Text;
frm1.Show();
this.Hide();
}
else
{
MessageBox.Show("Please insert some text...");
}
}
When I'm trying to register an user with username and password - it said it is successful, but the only thing that is added in the database is an empty row. 当我尝试使用用户名和密码注册用户时-表示成功,但是数据库中唯一添加的内容是空行。 When I just click on "Register" button, without writing something, the whole thing breaks up and this error comes:
当我只单击“注册”按钮而未编写任何内容时,整个过程就会崩溃,并且会出现此错误:
Violation of PRIMARY KEY constraint 'PK_ Login _536C85E5BD4FE4C6'.
违反主键约束'PK_ Login _536C85E5BD4FE4C6'。 Cannot insert duplicate key in object 'dbo.Login'.
无法在对象“ dbo.Login”中插入重复密钥。 The duplicate key value is ().
重复的键值为()。
Problem : you are only checking for null and Empty but You are not checking for Whitespace
. 问题:您仅检查null和Empty,但不检查
Whitespace
。
Solution : you need to also check for Whitespace
. 解决方案:您还需要检查
Whitespace
。 if you use String.IsNullOrWhiteSPace() it will check for Null,Empty and Whitespace. 如果使用String.IsNullOrWhiteSPace(),它将检查Null,Empty和Whitespace。
Try This: 尝试这个:
if(!String.IsNullOrWhiteSpace(textBox1.Text) || !String.IsNullOrWhiteSpace(textBox2.Text))
Suggestion : 建议:
the best way to confirm wether INSERT is successfull or not is checking the return
value of the ExecuteNonQuery()
method. 确认INSERT是否成功的最佳方法是检查
ExecuteNonQuery()
方法的return
值。
int Status=cmd.ExecuteNonQuery();
if(STatus>0)
MessageBox.Show("Your registration was successfull!");
else
MessageBox.Show("Please insert some text...");
Complete Code: 完整的代码:
private void button1_Click(object sender, EventArgs e)
{
if(!String.IsNullOrWhiteSpace(textBox1.Text) || !String.IsNullOrWhiteSpace(textBox2.Text))
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Dido\Documents\DataCars.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("insert into Login values('" + textBox1.Text + "','" + textBox2.Text + "')", con);
con.Open();
int Status=cmd.ExecuteNonQuery();
con.Close();
if(Status>0)
{
MessageBox.Show("Your registration was successfull!");
Login frm1 = new Login();
Global.GlobalVar = textBox1.Text;
frm1.Show();
this.Hide();
}
else
{
MessageBox.Show("no records updated!");
}
}
else
{
MessageBox.Show("Please insert some text...");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.