毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 数据库 >> 正文

C#中用update更新access数据库时有时不能成功

更新时间:2012-10-9:  来源:毕业论文

最近初学dataset,用它来更新access数据库时却发现了一件雷人的事:我在程序中,先通过代码,在内存中建立了dataset,并建立与之关联的表datatable,并向其中写入数据,然后建立access数据库,并建立了与之关联的一张空表FirstTable,建好后,我调用dataadapter更新access数据库,这是不可思议的事情发生了:更新有时能成功,有时不能成功,有时成功的时候竟然少了些记录,太让人蛋疼了。代码如下:
  DataSet dsClass1 = new DataSet();
  DataTable dtClass = new DataTable("Class");
  dsClass1.Tables.Add(dtClass);


  DataColumn iD = new DataColumn("ID", typeof(int));
  //dsClass1.Tables[0].PrimaryKey = new DataColumn[] { dtClass.Columns["ID"] };

  DataColumn className = new DataColumn();
  className.ColumnName = "ClassName";
  className.DataType = System.Type.GetType("System.String");
  className.MaxLength = 50;

  DataColumn gradeName = new DataColumn("GradeName", typeof(string));

 


  dtClass.Columns.Add("ID");
  dtClass.Columns.Add(className);
  dtClass.Columns.Add(gradeName);
  DataRow drClass1 = dtClass.NewRow();

  drClass1["ID"] = 1;
  drClass1["ClassName"] = "sdsd";
  drClass1["GradeName"] = "dssd";

  DataRow drClass2 = dtClass.NewRow();
  drClass2["ID"] = 2;
  drClass2["ClassName"] = "dss";
  drClass2["GradeName"] = "dsds";

  DataRow drClass3 = dtClass.NewRow();
  drClass3["ID"] = 3;
  drClass3["ClassName"] = "dsdf";
  drClass3["GradeName"] = "bgbz";

  //DataRow drClass3 = dtClass.NewRow();
  //drClass2["ID"] = 4;
  //drClass2["ClassName"] = "vdf";
  //drClass2["GradeName"] = "bgbz";
  //UniqueConstraint uc = new UniqueConstraint(d
  //dsClass.Tables["0"].Constraints.Add(uc);
  dtClass.Rows.Add(drClass1);
  dtClass.Rows.Add(drClass2);
  dtClass.Rows.Add(drClass3);
  //dtClass.Rows.Add(drClass3);
  textBox1.Text = dtClass.Rows[0]["ID"].ToString() + "\r\n" + dtClass.Rows[0]["ClassName"].ToString();
  textBox1.Text += "\r\n" + dtClass.Rows[0]["GradeName"].ToString();

  ADOX.Catalog catalog = new Catalog();
  catalog.Create("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = d:\\data\\test.mdb;Jet OLEDb:Engine Type = 5");
  ADODB.Connection cn = new ADODB.Connection();
  cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = d:\\data\\test.mdb", null, null, -1);
  catalog.ActiveConnection = cn;

  ADOX.Table table = new ADOX.Table();
  table.Name = "FirstTable";

 


  //string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\data\\test.mdb";
  //OleDbConnection olecon = new OleDbConnection(connStr);
  //olecon.Open();
  //OleDbDataAdapter oda = new OleDbDataAdapter("select * from Class", olecon);
  //OleDbCommandBuilder cmdbld = new OleDbCommandBuilder(oda);
  //cmdbld.SetAllValues = true;
  //oda.InsertCommand = cmdbld.GetInsertCommand();
  //dsClass.Tables[0].BeginLoadData();
  //int rowcount = dsClass.Tables[0].Rows.Count;
  //for (int i = 0; i < rowcount; i++)
  //{
  // dsClass.Tables[0].Rows[i].SetAdded();
  //}
  //dsClass.Tables[0].EndLoadData();
  //oda.Update(dsClass, "Class");
  //oda.Dispose();
  //olecon.Close();
  //olecon.Dispose();


  ADOX.Column column = new ADOX.Column();
  column.ParentCatalog = catalog;
  column.Name = "ID";
  column.Type = DataTypeEnum.adInteger;
  column.DefinedSize = 9;
  column.Properties["AutoIncrement"].Value = true;
  table.Columns.Append(column, DataTypeEnum.adInteger, 9);
  table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
  table.Columns.Append("ClassName", DataTypeEnum.adVarWChar, 10);
  table.Columns.Append("GradeName", DataTypeEnum.adVarWChar, 10);
  catalog.Tables.Append(table);
  string select = "select * from FirstTable";
  string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = d:\\data\\test.mdb";
  OleDbConnection odc = new OleDbConnection(connStr);
  OleDbDataAdapter dataAdapter = new OleDbDataAdapter(select, odc);
  OleDbCommandBuilder builder = new OleDbCommandBuilder(dataAdapter);
  dataAdapter.Update(dsClass1, "Class");
   
  MessageBox.Show("sdsd");

 ACCESS中有时通过ADO。NET插入的数据在等一会才能看到。 ADO操作MDB,有一定时间的延迟

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。