如何通过单击按钮将DataGridView数据中的更改发送到数据库(Access)?

时间:2015-07-03 10:45:43

标签: c# database winforms datagridview

我已经看到很多关于此的问题和答案,但是我的代码无法解决问题。

我以这种方式从数据库中显示表格:

public void refreshTable() {
            string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb";
            OleDbConnection connection = new OleDbConnection(conStr);
            OleDbDataAdapter adapter = new OleDbDataAdapter();

            connection.Open();
            OleDbCommand command = new OleDbCommand("SELECT * FROM " + 
                comboBox1.Text, connection); //comboBox1 have table name

            connection.Close();

            adapter.SelectCommand = command;
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            dataGridView1.DataSource = dataSet.Tables[0];
            adapter.Update(dataSet);
        }

每个人都这么说:http://tech.pro/tutorial/664/csharp-tutorial-binding-a-datagridview-to-a-database

我尝试以这种方式使用此链接中的代码:

private void saveButton_Click(object sender, EventArgs e) {
            string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb";
            OleDbConnection connection = new OleDbConnection(conStr);
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + 
                comboBox1.Text, connection);

            BindingSource bSource = new BindingSource();
            DataTable dTable = new DataTable();

            adapter.Fill(dTable);
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;

            adapter.Update(dTable);
        }

一切看起来很好,但是当我按下按钮(调用saveButton_Click)时,我在DataGridView中所做的所有更改都是撤消,数据库仍然是相同的。所以基本上它只是再次将DataGridView从数据库加载到DataGridView。

但如果我在DataGridView中写入不正确的内容(键值“asdasd”或日期“qwer”),程序会显示异常。

我做错了什么?

3 个答案:

答案 0 :(得分:0)

这是在VB和Mysql服务器中,但ADO的基础是相同的

Dim CS As New MySqlConnection(My.Settings.myConnectionString)
Dim da As New MySqlDataAdapter("SELECT * FROM " + 
            comboBox1.Text, CS)
Dim ds As New DataSet
Dim cmb As New MySqlCommandBuilder(da)
Dim tableNamesBS As New BindingSource

    Try
    da.Fill(ds, "test")
    datagridview1.DataSource = ds.Tables("test")
    tableNamesBS.DataSource = ds.Tables("test")

        Dim x As Integer
        da.UpdateCommand = cmb.GetUpdateCommand
        x = da.Update(ds.Tables("test"))
        MsgBox(x & "Record(s) Updated")
    Catch ex As Exception
        msgbox("Error in " & ex.message)
    End Try

答案 1 :(得分:0)

您正在“保存”按钮中再次加载和填充数据单击事件,这就是为什么所有更新的更改都将被清除。

使用公共变量在不同的子/函数中加载和保存(更新)事件。

所以LoadData()将是

//Global variable for adapter;
DataSet dataSet;
public void LoadData() {
            string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb";
            OleDbConnection connection = new OleDbConnection(conStr);
            OleDbDataAdapter adapter = new OleDbDataAdapter();

            connection.Open();
            OleDbCommand command = new OleDbCommand("SELECT * FROM " + 
                TABLE_NAME, connection); 

            connection.Close();

            adapter.SelectCommand = command;
            dataSet = new DataSet();
            adapter.Fill(dataSet);
            dataGridView1.DataSource = dataSet.Tables[0];

        }

然后在UpdateData函数

public void UpdateData()
{
    adapter.update(dataSet);
}

答案 2 :(得分:0)

最后得到它。

public FormExemple : Form
{
    protected string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb";
    protected OleDbConnection connection;
    protected OleDbDataAdapter adapter;
    ....

    Window_Load()
    {
        connection = new OleDbConnection(conStr);
        ...
        adapter.Fill(dTable);

        cb = new OleDbCommandBuilder(adapter);
        adapter.InsertCommand = cb.GetInsertCommand();
        //and other commands
        ...
    }

    Button_Click()
    {
        ...
        adapter.Update(dTable)
        ...
    }
}