C# - 微软 sql 数据库 |无法在我的电子联系人应用程序上添加新用户

C# - Microsoft sql database | Can't add new user on my e-contact app

我尝试在 Visual Studio 2019 上使用 C# 制作一个电子联系人应用程序,并按照 youtube 教程连接到 Miscrosoft SQL 数据库(本地)。

该应用程序尚未完成,无论如何 btnAdd 应该可以工作,但它不会添加用户和方法 (Insert) 的 return。

它总是 returns false - 谁能帮助我?

private void BntAdd_Click(object sender, EventArgs e) {
    //Get the value from the imput fields
    c.Nome = txtBoxName.Text;
    c.Cognome = txtBoxSurname.Text;
    c.Telefono1= txtBoxPhone1.Text;
    c.Telefono = txtBoxPhone.Text;
    c.Email = txtBoxEmail.Text;

    //Inserting Data into Database uing the method we created is previous episode
    bool success = c.Insert(c);
    if (success == true)
    {
        //Successfully Inserted
        MessageBox.Show("New contact added!");
        //Call the clear Method Here
        Clear();
    }
    else
    {
        //Failed to add Contact
        MessageBox.Show("ERROR!)");
    }
    //load Data on Data GRidview
    DataTable dt = c.Select();
    dgvRubrica.DataSource = dt;
}

public void Clear()
{
    txtBoxName.Text = "";
    txtBoxSurname.Text = "";
    txtBoxPhone1.Text = "";
    txtBoxPhone.Text = "";
    txtBoxEmail.Text = "";
}

public bool Insert (rubricaClass c) {
    bool isSuccess = false;

    SqlConnection conn = new SqlConnection(myconnstrng);
    try
    {
        string sql = "INSERT INTO tbl_Rubrica (Nome, Cognome, Telefono1, Telefono, Email) VALUES (@Nome, @Cognome, @Telefono1, @Telefono, @Email)";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@Nome", c.Nome);
        cmd.Parameters.AddWithValue("@Cognome", c.Cognome);
        cmd.Parameters.AddWithValue("@Telefono1", c.Telefono1);
        cmd.Parameters.AddWithValue("@Telefono", c.Telefono);
        cmd.Parameters.AddWithValue("@Email", c.Email);

        conn.Open();
        int rows = cmd.ExecuteNonQuery();

        if (rows > 0)
        {
            isSuccess = true;  
        }
        else
        {
            isSuccess = false;
        }
    }
    catch (Exception ex)
    {

    }
    finally
    {
        conn.Close();
    }
    return isSuccess;
}

它没有给出任何错误,它工作但是当我将 ata 键入 txtBoxes 然后我按下添加按钮时它说错误(在其他地方插入消息框)

第 1 步是从 Insert 方法中删除 catch-all 异常处理。大多数 ADO.NET 数据库 类 实现 IDisposable,所以你只需要一个 using(...) 块来确保自动处理命令(这也会关闭并处理连接实例):

public bool Insert (rubricaClass c) 
{
    bool isSuccess = false;

    SqlConnection conn = new SqlConnection(myconnstrng);

    string sql = "INSERT INTO tbl_Rubrica (Nome, Cognome, Telefono1, Telefono, Email) VALUES (@Nome, @Cognome, @Telefono1, @Telefono, @Email)";
    using(SqlCommand cmd = new SqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@Nome", c.Nome);
        cmd.Parameters.AddWithValue("@Cognome", c.Cognome);
        cmd.Parameters.AddWithValue("@Telefono1", c.Telefono1);
        cmd.Parameters.AddWithValue("@Telefono", c.Telefono);
        cmd.Parameters.AddWithValue("@Email", c.Email);

        conn.Open();
        int rows = cmd.ExecuteNonQuery();

        if (rows > 0)
        {
            isSuccess = true;  
        }
        else
        {
            isSuccess = false;
        }
    }

    return isSuccess;
}

确定后,第 2 步是将异常处理移至应用程序中。我不推荐这种 "catch everything" 风格的代码,但我想它现在可以工作:

private void BntAdd_Click(object sender, EventArgs e) 
{
    //Get the value from the imput fields
    c.Nome = txtBoxName.Text;
    c.Cognome = txtBoxSurname.Text;
    c.Telefono1= txtBoxPhone1.Text;
    c.Telefono = txtBoxPhone.Text;
    c.Email = txtBoxEmail.Text;

    try
    {
        //Inserting Data into Database uing the method we created is previous episode
        bool success = c.Insert(c);
        if (success == true)
        {
            //Successfully Inserted
            MessageBox.Show("New contact added!");
            //Call the clear Method Here
            Clear();
        }
        else
        {
            //Failed to add Contact
            MessageBox.Show("ERROR!)");
        }
        //load Data on Data GRidview
        DataTable dt = c.Select();
        dgvRubrica.DataSource = dt;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

这可能会告诉您您的 SQL 语法有误,或者命令本身不能是 运行(即连接字符串无效或服务器不能'达不到)。