将记录插入MS Access

时间:2014-12-11 21:43:36

标签: c# ms-access oledb

有人可以向我解释为什么它不允许我将我的记录插入MS Access? 出于某种原因,我已经尝试了人类可能做到的一切,但它不会让我做我需要做的事情。我需要做的是允许记录从我的夏普程序写入,并希望按下一个按钮,将值插入我的MS Access。

void Insert_Record(object s, EventArgs e)
{
    string dbconnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"data source =BookCSharp.accdb";
    string dbcommand = "INSERT INTO BookKey, Title, Pages from Books;";
    OleDbConnection conn = new OleDbConnection(dbconnection);
    OleDbCommand comm = new OleDbCommand(dbcommand, conn);
    OleDbDataAdapter adapter = new OleDbDataAdapter(comm);          

    conn.Open();

    adapter.Fill(ds, "Books");

    conn.Close();
    DataTable dt = ds.Tables[0];
    //define a new record and place it into a new DataRow
    DataRow newRow   = dt. NewRow();
    newRow["BookKey"] = txtBookKey.Text;
    newRow["Title"]  = txtTitle.Text;
    newRow["Pages"]  = txtPages.Text;

    //add the new DataRow to DataTable 
    dt.Rows.Add(newRow);

    //update DB
    adapter.Update(ds, "Books");

    //accept changes
    ds.AcceptChanges();

    //update listBox1
    lstDisplayBooks.Items.Clear();
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        lstDisplayBooks.Items.Add(row["BookKey"] + " " + row["Title"] + " (" + row["Pages"] + ")");
    }
    txtBookKey.Enabled = false;
    txtBookKey.Text = " ";
    txtTitle.Enabled = false;
    txtTitle.Text = " ";
    txtPages.Enabled = false;
    txtPages.Text = " ";
    btnInsert.Enabled = false;
}

1 个答案:

答案 0 :(得分:0)

OleDbDataAdapter需要SELECT查询和OleDBCommandBuilder自动生成将在Update方法中使用的INSERT / UPDATE / DELETE命令

void Insert_Record(object s, EventArgs e)
{
    string dbconnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"data source =BookCSharp.accdb";
    string dbcommand = "SELECT * FROM Books;";
    using(OleDbConnection conn = new OleDbConnection(dbconnection))
    using(OleDbCommand comm = new OleDbCommand(dbcommand, conn))
    {
         OleDbDataAdapter adapter = new OleDbDataAdapter(comm);
         OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
         // See comments below for these properties
         builder.QuotePrefix = "[";
         builder.QuoteSuffix = "]";
         conn.Open();            
         adapter.Fill(ds, "Books");

         DataTable dt = ds.Tables[0];
         DataRow newRow   = dt. NewRow();
         newRow["BookKey"] = txtBookKey.Text;
         newRow["Title"]  = txtTitle.Text;
         newRow["Pages"]  = txtPages.Text;
         dt.Rows.Add(newRow);
         builder.GetInsertCommand();
         adapter.Update(ds, "Books");
    }

    //update listBox1
    lstDisplayBooks.Items.Clear();
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        lstDisplayBooks.Items.Add(row["BookKey"] + " " + row["Title"] + " (" + row["Pages"] + ")");
    }
    txtBookKey.Enabled = false;
    txtBookKey.Text = " ";
    txtTitle.Enabled = false;
    txtTitle.Text = " ";
    txtPages.Enabled = false;
    txtPages.Text = " ";
    btnInsert.Enabled = false;
}

OleDbCommandBuilder链接中的示例包含这样的示例。

代码应该修复你的初始错误,但实际上在这种情况下不需要使用OleDbDataAdapter。如果您的唯一目标只是向数据表添加新记录,而您不需要像DataGridView那样维护本地数据源,那么使用适当的写入INSERT和参数化查询直接使用OleDbCommand会更加简单

void Insert_Record(object s, EventArgs e)
{
    string dbconnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"data source =BookCSharp.accdb";
    string dbcommand = "INSERT INTO Books (BookKey, Title, Pages) VALUES(?,?,?);";
    using(OleDbConnection conn = new OleDbConnection(dbconnection))
    using(OleDbCommand comm = new OleDbCommand(dbcommand, conn))
    {
         conn.Open();            
         comm.Parameters.AddWithValue("@p1", txtBookKey.Text);
         comm.Parameters.AddWithValue("@p2", txtTitle.Text);
         comm.Parameters.AddWithValue("@p3", txtPages.Text);
         comm.ExecuteNonQuery();
    }
    .....