简体   繁体   English

注册表单Visual Studio中的SQL Server数据库问题

[英]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.

相关问题 从 Visual Studio 连接到 SQL Server 数据库的问题 - Issue with connecting to SQL Server database from Visual Studio 插入登录和注册数据未显示在 SQL 服务器数据库表 Visual Studio 2019 - Insert login and registration data not show in SQL Server database table Visual Studio 2019 在Visual Studio中创建注册表单,SQL语法出错 - Creating Registration form in Visual Studio, getting error in SQL Syntax Visual Studio-表单未使用sql数据库更新 - visual studio - form not updating with sql database Visual Studio和SQL Server。 无法将数据库导入VIsual Studio - Visual Studio and SQL Server . Cannot see Database into VIsual Studio 在Visual Studio中创建SQL Server数据库时出错 - Error creating an SQL Server Database in Visual Studio 在Visual Studio中使用SQL Server中的数据库 - Using Database from SQL Server in Visual Studio 与Visual Studio 2015中的SQL Server数据库进行交互 - Interacting with SQL Server database in Visual Studio 2015 在窗体C#Visual Studio中比较两个日期起始日期和数据库SQL Server中的日期 - Compare Two Date From Date at Form C# Visual Studio and Date From Database SQL Server 如何在SQL Server Management Studio中使用Visual Studio创建连接数据库? - How connect database created in SQL Server Management Studio with Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM