0

I am trying to insert records into an Access table from a SQL database. When I step through, I add 5 rows but when I am done, the rows aren't in the table. I know I am missing something easy, but I sure can't figure it out.

OdbcConnection ocn = new OdbcConnection
("Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\my.mdb;Uid=Admin;Pwd=;");
ocn.Open();
OdbcDataAdapter adapter = new OdbcDataAdapter
    (@"SELECT * FROM FROM Nodes Order by Node ASC", ocn);

da.InsertCommand = new OdbcCommand(@"INSERT INTO NODES(Node,Descriptor,NDisabled,GroupNum,LocalAddress
                            TurnOffPolling,CommOffSig,MaintSig,CollectAuditSig,SkipHistory,KeepAwake,DialUp,KeepAwakeDate,ModelNumber)
                            VALUES(@Node,@Descriptor,@NDisabled,@Groupnum,@LocalAddress,
                            @TurnOffPolling,@CommOffSig,@maintSig,@CollectAuditSig,@SkipHistory,@KeepAwake,@DialUp,@KeepAwakeDate,@ModelNumber)", ocn);
                        da.InsertCommand.Parameters.Add("@Node", OdbcType.VarChar);
                        da.InsertCommand.Parameters.Add("@Description", OdbcType.VarChar);
                        da.InsertCommand.Parameters.Add("@NDisabled", OdbcType.Bit);
                        da.InsertCommand.Parameters.Add("@GroupNum", OdbcType.Int);
                        da.InsertCommand.Parameters.Add("@LocalAddress", OdbcType.Int);
                        da.InsertCommand.Parameters.Add("@TurnOffPolling", OdbcType.Bit);
                        da.InsertCommand.Parameters.Add("@CommOffSig", OdbcType.VarChar);
                        da.InsertCommand.Parameters.Add("@MaintSig", OdbcType.VarChar);
                        da.InsertCommand.Parameters.Add("@CollectAuditSig", OdbcType.VarChar);
                        da.InsertCommand.Parameters.Add("@SkipHistory",OdbcType.Bit);
                        da.InsertCommand.Parameters.Add("@KeepAwake",OdbcType.Bit);
                        da.InsertCommand.Parameters.Add("@DialUp",OdbcType.Bit);
                        da.InsertCommand.Parameters.Add("@KeepAwakeDate",OdbcType.VarChar);
                        da.InsertCommand.Parameters.Add("@ModelNumber",OdbcType.Int);

DataTable tbl = new System.Data.DataTable();
adapter.Fill(tbl);
while(SqlDataReader.Read())
{
    DataRow nrow = tbl.NewRow();
        nrow["Node"] = rdr["Node"];
    tbl.Rows.Add(nrow); //I see the tbl.Rows.Count increase by one here
}
tbl.AcceptChanges();
adapter.Update(tbl);

ocn.Close();

When I open the table, the records aren't in the table. Can somebody tell me what I'm doing wrong here please?

Thanks in advance, Bob

4

1 回答 1

0

改变:

tbl.AcceptChanges();
adapter.Update(tbl);

adapter.Update(tbl);
tbl.AcceptChanges();

而且我看不到您在哪里为您提供OdbcDataAdapterInsertCommandUpdateCommandor DeleteCommand)来执行数据库表的适当更新。

于 2013-01-04T19:38:26.107 回答