首页 > 解决方案 > 我的 Visual Studio 项目正在删除和编辑数据库中的项目 OK,但是在点击保存时它不会添加记录

问题描述

我的 Visual Studio 项目正在删除和编辑数据库中的项目正常,但是在点击保存时它不会添加新记录。我认为它可以输入,但是一旦我点击保存它就不会保存插入的记录。

public partial class Form1 : Form
{
    SqlConnection sqlConnection = null;

    public void connect()
    {
        sqlConnection = new SqlConnection();
        sqlConnection.ConnectionString = @"Data Source=(localdb)\ProjectsV13;Initial Catalog=mydatabase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        sqlConnection.Open();
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        connect();

        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = "SELECT * FROM [Table]";

        SqlDataReader dataReader = sqlCommand.ExecuteReader();

        dataGridView1.Columns.Add("Id", "ID");
        dataGridView1.Columns.Add("Name", "NAME");
        dataGridView1.Columns.Add("Email", "EMAIL");

        for (int i = 0; dataReader.Read(); i++)
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[i].Cells["ID"].Value = dataReader["Id"].ToString();
            dataGridView1.Rows[i].Cells["NAME"].Value = dataReader["Name"].ToString();
            dataGridView1.Rows[i].Cells["EMAIL"].Value = dataReader["Email"].ToString();
        }

        sqlConnection.Close();
    }

    private void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value.ToString());

        string name = dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value.ToString();
        string email = dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value.ToString();

        connect();

        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = "insert into [Table] (Id, Name, Email) values (" + id + ",'" + name + "','" + email + "')";
        sqlCommand.CommandText = "SET IDENTITY_INSERT [Table] ON";

        sqlCommand.ExecuteNonQuery();

        sqlConnection.Close();
        MessageBox.Show("Added");
    }

    private void Button3_Click(object sender, EventArgs e)
    {
        connect();

        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = "Delete from [Table] where Id =" + dataGridView1.Rows[selectedid].Cells["ID"].Value;

        sqlCommand.ExecuteNonQuery();

        sqlConnection.Close();

        dataGridView1.Rows.RemoveAt(selectedid);
        MessageBox.Show("Record Deleted");
    }

    int selectedid = 0;

    private void DataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        selectedid = e.RowIndex;
    }

    private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            string name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            string email = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

            connect();

            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "update [Table] set Name = '" + name + "', Email ='" + email + "' Where Id =" + id;

            sqlCommand.ExecuteNonQuery();

            sqlConnection.Close();
        }
        catch (Exception ex)
        {
        }
    }
}

我对 C# 语言很陌生,希望有更多经验的人能够帮助我。

标签: c#databasevisual-studio

解决方案


推荐阅读