日期:2014-05-20  浏览次数:21084 次

使用DataTable更新access
从页面上传过来一个Datatable ,然后用这个插入到数据库里,
public void Setrcjqdde(DataTable dt0)
  {
  string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\业务操作\RationLibary.mdb;";
  using (OleDbConnection conn = new OleDbConnection(connectionstring))
  {
  try
  {
  conn.Open();
  string sql = "delete from jsqdde";
  OleDbCommand cmd = new OleDbCommand(sql, conn);
  cmd.ExecuteNonQuery();
  OleDbDataAdapter da = new OleDbDataAdapter();
   
  OleDbCommand cmd2 = new OleDbCommand("insert into jsqdde (bm,nr,dw,dj,dj1) values (?,?,?,?,?)");
  OleDbDataAdapter adapt = new OleDbDataAdapter("select bm,nr,dw,dj,dj1 from jsqdde", conn);

  OleDbCommandBuilder OleDbCmdBud = new OleDbCommandBuilder(adapt);
  OleDbParameter bm = new OleDbParameter("@bm", OleDbType.VarChar, 50);
  OleDbParameter nr = new OleDbParameter("@nr", OleDbType.VarWChar, 50);
  OleDbParameter dw = new OleDbParameter("@dw", OleDbType.VarWChar, 10);
  OleDbParameter dj = new OleDbParameter("@dj", OleDbType.Single);
  OleDbParameter dj1 = new OleDbParameter("@dj1", OleDbType.Single);
  adapt.Fill(dt0);  
  adapt.SelectCommand.Parameters.Add(bm);
  adapt.SelectCommand.Parameters.Add(nr);
  adapt.SelectCommand.Parameters.Add(dw);
  adapt.SelectCommand.Parameters.Add(dj);
  adapt.SelectCommand.Parameters.Add(dj1);  
  adapt.SelectCommand.Parameters["@bm"].SourceColumn = "bm";
  adapt.SelectCommand.Parameters["@nr"].SourceColumn = "nr";
  adapt.SelectCommand.Parameters["@dw"].SourceColumn = "dw";
  adapt.SelectCommand.Parameters["@dj"].SourceColumn = "dj";
  adapt.SelectCommand.Parameters["@dj1"].SourceColumn = "dj1";

  adapt.InsertCommand = OleDbCmdBud.GetInsertCommand();
  if (dt0 != null)
  {
  try
  {
  adapt.Update(dt0);
  }
  catch (Exception ex)
  {

  }
  }
  }
  catch (Exception ex)
  {
  }
  finally
  {
  if (conn.State == ConnectionState.Open)
  {
  conn.Close();
  }
  dt0.Clear();
  }
  }
  }
有主键是ID

------解决方案--------------------
批量插入的方法
http://dotnet.aspx.cc/file/Insert-DataSet-Into-Access.aspx
------解决方案--------------------
我传过来的是DataTable,所以这个value到底要怎么写。
我是要把Datatable里的值批量插入到access中