使用OleDB保存到DataBase

时间:2015-12-15 17:13:52

标签: c# database save oledb

我很难理解OLEDB,我特别努力获取信息以保存到数据库这是我当前尝试保存的代码:

注意:我可以非常正确地加载信息,当我运行此功能时,我的信息出现在我的listBox中,但是在关闭应用程序时它还没有保存。

如果你能帮助解释那个很棒的问题

void Insert_New_Log(int startfloor, int endfloor, string currentAction)
    {
        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();

        conn.Open();


        adapter.Fill(ds, "ElevatorTable");

        conn.Close();



        DataRow newRow = ds.Tables[0].NewRow();
        newRow["ID"] = 0;
        newRow["Date1"] = dateAndTime;
        newRow["StartingFloor"] = startfloor;
        newRow["EndFloor"] = endfloor;
        newRow["Action"] = currentAction;

        ds.Tables[0].Rows.Add(newRow);

        DataSet dataSetChanges = ds.GetChanges();

        try
        {
            adapter.Update(dataSetChanges, "ElevatorTable");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

        ds.AcceptChanges();



        //update Visible list

        dbListBox.Items.Clear();
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            dbListBox.Items.Add(row["ID"] + "\t" + row["Date1"] + "\t" + row["StartingFloor"] + "\t" + row["EndFloor"] + "\t" + " (" + row["Action"] + ")");
        }

    }

我已经写了一行写入并发现了这个异常。 抛出异常:' System.Data.OleDb.OleDbException'在System.Data.dll中 System.Data.OleDb.OleDbException(0x80040E14):INSERT INTO语句中的语法错误。    在System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent,BatchCommandInfo [] batchCommands,Int32 commandCount)    在System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent,BatchCommandInfo [] batchCommands,Int32 commandCount)    在System.Data.Common.DbDataAdapter.Update(DataRow [] dataRows,DataTableMapping tableMapping)    在System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable,DataTableMapping tableMapping)    在System.Data.Common.DbDataAdapter.Update(DataSet dataSet,String srcTable)    在System.Data.Common.DbDataAdapter.Update(DataSet dataSet)    在Elevator.Form1.Insert_New_Log(Int32 startfloor,Int32 endfloor,String currentAction)中的C:\ Users \ Brads \ Desktop \ Elevator \ Elevator \ Form1.cs:第197行

2 个答案:

答案 0 :(得分:1)

填写适配器后,您已关闭连接。您需要打开连接或重新打开它。

adapter.Fill(ds, "ElevatorTable"); conn.Close();

按照这个 conn.Open();

答案 1 :(得分:0)

您在适配器中设置的用户是否在SQL数据库中具有写入权限?此外,您还需要在更新语句之前关闭连接,以便回写。

void Insert_New_Log(int startfloor, int endfloor, string currentAction)
{
    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();

    conn.Open();


    adapter.Fill(ds, "ElevatorTable");        

    DataRow newRow = ds.Tables[0].NewRow();
    newRow["ID"] = 0;
    newRow["Date1"] = dateAndTime;
    newRow["StartingFloor"] = startfloor;
    newRow["EndFloor"] = endfloor;
    newRow["Action"] = currentAction;

    ds.Tables[0].Rows.Add(newRow);

    DataSet dataSetChanges = ds.GetChanges();
    ds.AcceptChanges();

    try
    {
        adapter.Update(dataSetChanges, "ElevatorTable");

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally { conn.Close(); }

    //update Visible list

    dbListBox.Items.Clear();
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        dbListBox.Items.Add(row["ID"] + "\t" + row["Date1"] + "\t" + row["StartingFloor"] + "\t" + row["EndFloor"] + "\t" + " (" + row["Action"] + ")");
    }

}