【问题标题】:I can't seem to add or remove from database and update the datagrid based on the database. Why?我似乎无法在数据库中添加或删除并根据数据库更新数据网格。为什么?
【发布时间】:2014-11-09 16:30:12
【问题描述】:

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

按钮 1 问题 当我按下按钮 1(将记录添加到数据库并在数据网格中重新加载数据库)时,它在第一次单击按钮 1 时起作用,当我更改文本框中的文本(标题、作者、股票)以添加到数据库并再次单击按钮时,它不会改变添加到数据库中的内容。 例子 textbox1.text(title)= "哈利波特" textbox2.text(author)="jk" textbox3.text(stock)="3" 按下按钮 1(添加)。在第一次单击 button1 时工作正常。当我更改 textbox1-3 的值时 例子 textbox1.text(title)="alchemist" textbox2.text(author)="paollo cuello" textbox3.text(stock)="5" 它仍然记录“哈利波特”、“jk”和“3”。

Button2 问题 当我按下按钮 2(删除数据库中的记录并在数据网格中重新加载数据库)时,它在第一次单击时起作用,但并没有真正显示在数据网格上。 例子。 数据网格显示值 标题作者股票 “哈利波特”“jk”3 和 《炼金术士》《电脑》4

textbox1.text(title)="harrypotter" textbox2.text(author)="jk" textbox3.text(stock)="3" button2(删除并重新加载数据网格)被按下。 结果 = datagrid 不加载新记录,但哈利波特记录在数据库中被删除 当我尝试删除另一条记录时。 例子。 标题作者股票 哈利波特 jk 3 炼金术士 pc 4

请注意“哈利波特”仍然存在,因为重新加载不起作用。 textbox1.text(title)="alchemst" textbox2.text(author)="pc" textbox3.text(stock)="4" button2(删除并重新加载数据网格)被按下。 它不起作用。我敢打赌它试图删除“哈利波特记录仍然”

为什么?

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";
            }
    }
}

【问题讨论】:

    标签: c# database datagrid insert sql-delete


    【解决方案1】:

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

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

    title = harrypotter 的示例。

    这是给按钮 1 的。

    【讨论】:

    • 但我无意更新任何记录。我所指的更新是保存数据库的数据网格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2021-08-31
    相关资源
    最近更新 更多