我可以';t似乎是在数据库中添加或删除,并基于数据库更新数据网格.为什么?

本文关键字:数据库 更新 数据 为什么 网格 数据网 似乎是 我可以 添加 删除 | 更新日期: 2023-09-27 18:22:02

我正在开发一个库数据库,您可以在该数据库中添加和删除图书。

按钮1问题当我按下按钮1(将记录添加到数据库并在数据网格中重新加载数据库)时,它在第一次单击按钮1时工作,当我更改文本框中要添加到数据库的文本(标题、作者、股票)并再次单击按钮时,它不会更改添加到数据库中的内容。实例textbox1.text(title)="哈利波特"textbox2.text(author)="jk"textbox3.text(stock)="3"按下按钮1(添加)。第一次点击按钮1时效果良好。当我更改文本框1-3的值时实例textbox1.text(title)="炼金术士"textbox2.text(author)="paollo cuello"textbox3.text(stock)="5"它仍然记录着"哈利波特"、"jk"answers"3"。

按钮2问题当我按下按钮2(删除数据库中的记录并在数据网格中重新加载数据库)时,它在第一次单击时起作用,但并没有真正显示在数据网格上。实例datagrid显示值标题作者股票"harrypotter"jk"3和"炼金术士"pc"4

textbox1.text(title)="harrypotter"textbox2.text(author)="jk"textbox3.text(stock)="3"按下按钮2(移除并重新加载数据网格)。result=datagrid没有加载新记录,但数据库中的哈利波特记录已被删除当我尝试删除另一条记录时。实例标题作者股票哈里波特jk 3炼金术士pc 4

注意,"harrypotter"仍然存在,因为重新加载不起作用。textbox1.text(title)="alchemst"textbox2.text(author)="pc"textbox3.text(stock)="4"按下按钮2(移除并重新加载数据网格)。它不起作用。我打赌它试图删除"harrypotter记录静止"

为什么?

public partial class AddBook : Form
{
    String title = "", author = "";
    bool hasValue1 = false, hasValue2 = false, hasValue3 = false;
    string holder = "";
    int stock = 0;
    OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'Jc'Documents'Visual Studio 2013'Projects'itswn'Library System'Library System'LibrarySystemDatabase.accdb;Persist Security Info=True");
    OleDbCommand command = new OleDbCommand();
    OleDbDataReader reader;
    public AddBook()
    {
        InitializeComponent();
    }
    private void AddBook_Load(object sender, EventArgs e)
    {
        dataGridView1.Columns.Add("Title", "Title");
        dataGridView1.Columns.Add("Author", "Author");
        dataGridView1.Columns.Add("Stock", "Stock");
        connect.Open();
        loaddataBook();
    }

    private void loaddataBook()
    {
        int i = 0;
        try
        {
            command.CommandText = "SELECT Title, Author, Stock FROM Book";
            command.Connection = connect;
            reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[i].Cells["Title"].Value = reader[0].ToString();
                    dataGridView1.Rows[i].Cells["Author"].Value = reader[1].ToString();
                    dataGridView1.Rows[i].Cells["Stock"].Value = reader[2].ToString();
                    i++;
                }
            }
            reader.Close();
            connect.Close();
        }
        catch (OleDbException ex)
        {
            connect.Close();
            MessageBox.Show(ex.ToString());
        }
        connect.Close();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        title = textBox1.Text.ToLower();
        author = textBox2.Text.ToLower();
        stock = int.Parse(textBox3.Text);
        if (textBox1.Text != "")
        {
            hasValue1 = true;
        }
        if (textBox2.Text != "")
        {
            hasValue2 = true;
        }
        if (textBox3.Text != "")
        {
            hasValue3 = true;
        }
        if (int.Parse(textBox3.Text) >= 0 || textBox3.Text == "")
        {
            if (hasValue1 && hasValue2 && hasValue3)
            {
                try
                {
                    connect.Open();
                    command.Connection = connect;
                    command.Parameters.AddWithValue("@title", title);
                    command.Parameters.AddWithValue("@author", author);
                    command.Parameters.AddWithValue("@stock", stock);
                    command.CommandText = "INSERT INTO [Book](Title, Author, Stock) VALUES(@title, @author, @stock)";
                    command.ExecuteNonQuery();
                    loaddataBook();
                    connect.Close();
                }
                catch (OleDbException ex)
                {
                    connect.Close();
                    MessageBox.Show(ex.ToString());
                }
                connect.Close();
            }
            else
            {
                label4.Text = "required";
                label5.Text = "required";
                label6.Text = "required";
            }
        }
        else
        {
            MessageBox.Show("Stock should not be less than 0", "Keep in mind");
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        title = textBox1.Text.ToLower();
        author = textBox2.Text.ToLower();
        textBox3.Clear(); 
        if (textBox1.Text != "")
        {
            hasValue1 = true;
        }
        if (textBox2.Text != "")
        {
            hasValue2 = true;
        }
        if (hasValue1 && hasValue2)
        { 
                try
                {
                    connect.Open();
                    command.Connection = connect;
                    command.Parameters.AddWithValue("@title", title);
                    command.Parameters.AddWithValue("@author", author);
                    command.CommandText = "DELETE FROM [Book] WHERE Title = @title AND Author = @author";
                    command.ExecuteNonQuery();
                    loaddataBook();
                    connect.Close();
                }
                catch (OleDbException ex)
                {
                    connect.Close();
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                label4.Text = "required";
                label5.Text = "required";
            }
    }
}

我可以';t似乎是在数据库中添加或删除,并基于数据库更新数据网格.为什么?

您应该使用sql的更新函数而不是插入函数,例如

update [tablename]
set [Book] = [valueof book] , [author ] = [valueofauthor] , so on~~
where statement  you want to update~~~~ 

示例,其中title=harrypotter。

这是给纽扣1的。