OleDbConnection Connection;
string selectText = "select * from test";
string connectString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source=D:\test.mdb";
Connection=new OleDbConnection(connectString); System.Data.OleDb.OleDbDataAdapter da1 = new System.Data.OleDb.OleDbDataAdapter(selectText,Connection);
Connection.Open();
m_ds = new DataSet();
DataSet ds = m_ds;
da1.Fill (ds,"test");//
.......
//System.Data.OleDb.OleDbCommandBuilder dc = new System.Data.OleDb.OleDbCommandBuilder(da1);
da1.Update(ds,"test");
Connection.Close();运行代码后,出现以下的提示
未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中主要就是对数据库中的数据进行批量的修改,查了N多资料,表中有主键
string selectText = "select * from test";
string connectString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source=D:\test.mdb";
Connection=new OleDbConnection(connectString); System.Data.OleDb.OleDbDataAdapter da1 = new System.Data.OleDb.OleDbDataAdapter(selectText,Connection);
Connection.Open();
m_ds = new DataSet();
DataSet ds = m_ds;
da1.Fill (ds,"test");//
.......
//System.Data.OleDb.OleDbCommandBuilder dc = new System.Data.OleDb.OleDbCommandBuilder(da1);
da1.Update(ds,"test");
Connection.Close();运行代码后,出现以下的提示
未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中主要就是对数据库中的数据进行批量的修改,查了N多资料,表中有主键
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;namespace DownLoadFile
{
/// <summary>
/// Summary description for FrmOleDbUpdate.
/// </summary>
public class FrmOleDbUpdate : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
OleDbDataAdapter daAda;
DataSet ds ;
OleDbConnection Connection; public FrmOleDbUpdate()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
string selectText = "select * from test";
string connectString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source=e:\test.mdb";
Connection=new OleDbConnection(connectString);
daAda = new OleDbDataAdapter(selectText,Connection);
Connection.Open();
ds=new DataSet();
daAda.Fill (ds,"test");
this.dataGrid1.DataSource=ds.Tables["test"];
//
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(72, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(320, 280);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 328);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "更新";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// FrmOleDbUpdate
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 421);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "FrmOleDbUpdate";
this.Text = "FrmOleDbUpdate";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
OleDbCommandBuilder cb=new OleDbCommandBuilder(daAda);
try
{
daAda.Update(ds,"test");//这个地方如果不设主键错
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
this.dataGrid1.DataSource=ds.Tables["[Test]"];
这个正解啊,多谢!