C# 注册表单Visual Studio中的SQL Server数据库出现问题
这是我的代码:C# 注册表单Visual Studio中的SQL Server数据库出现问题,c#,sql-server,database,winforms,C#,Sql Server,Database,Winforms,这是我的代码: 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");
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...");
}
}
当我试图用用户名和密码注册一个用户时,它说注册成功了,但数据库中只添加了一个空行。当我只点击“注册”按钮,没有写任何东西时,整个事情就结束了,这个错误出现了:
违反主键约束“PK_Login_536C85E5BD4FE4C6”。无法在对象“dbo.Login”中插入重复的密钥。重复的键值为()
问题:您只检查null和Empty,而没有检查
空白
解决方案:您还需要检查空白。如果使用String.IsNullOrWhiteSPace(),它将检查Null、Empty和Whitespace
试试这个:
if(!String.IsNullOrWhiteSpace(textBox1.Text) || !String.IsNullOrWhiteSpace(textBox2.Text))
建议:
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...");
}
}
确认插入是否成功的最佳方法是检查ExecuteNonQuery()
方法的return
值
int Status=cmd.ExecuteNonQuery();
if(STatus>0)
MessageBox.Show("Your registration was successfull!");
else
MessageBox.Show("Please insert some text...");
完整代码:
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...");
}
}
请经常使用。这种类型的字符串连接容易受到攻击。流程看起来不正确:在尝试更新数据库之前,您不应该检查string.IsNullOrEmpty
吗?哦,谢谢,这很有效@凯西:不客气,很高兴能帮助你。