使用数据集更新Access数据库

时间:2018-11-14 14:59:14

标签: c#

我正在写这是因为我在使用数据集更新访问数据库时遇到问题。我没有收到任何错误,但数据库似乎没有更新。 这是我的代码:

public partial class Form1 : Form{
private DataSet ds = new DataSet();
string dbconnection = "Provider=Microsoft.JET.OLEDB.4.0;" + @"data source =../../database/Log1.mdb";
string dbcommand = "SELECT * FROM Log";

private void addRow(int action)
    {
        string actionCase = "Uknown action";
        switch (action)
        {
            case 1:
                actionCase = "Nothing";
                break;
            case 2:
                actionCase = "Is";
                break;
            case 3:
                actionCase = "Working";
                break;
            case 4:
                actionCase = "what";
                break;
        }
        DataRow row = ds.Tables["Log"].NewRow();
        row["action"] = actionCase;
        row["actionDateAndTime"] = DateTime.Now.ToString();
        ds.Tables["Log"].Rows.Add(row);
        ds.AcceptChanges();
    }

    private void button_Click(object sender, EventArgs e)
    {
        addRow(1);
        OleDbConnection conn = new OleDbConnection(dbconnection);
        OleDbCommand comm = new OleDbCommand(dbcommand, conn);
        OleDbDataAdapter adapter = new OleDbDataAdapter(comm);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
        adapter.UpdateCommand = builder.GetUpdateCommand();
        adapter.Update(ds, "Log");
    }

数据库有3列,ID为主键,action和actionDateAndTime都设置为短文本。

0 个答案:

没有答案