C# OleDbDataAdapter:无法更新数据库
我从昨天开始就一直在做这个,我就是不能更新我的数据库。 有三张桌子。这是WinForms之一的一段代码。它加载数据并显示,但在网格中手动更改某个内容后,我通过调用Update得到错误,或者发生任何事情 请帮帮我,我快疯了C# OleDbDataAdapter:无法更新数据库,c#,winforms,oledbdataadapter,C#,Winforms,Oledbdataadapter,我从昨天开始就一直在做这个,我就是不能更新我的数据库。 有三张桌子。这是WinForms之一的一段代码。它加载数据并显示,但在网格中手动更改某个内容后,我通过调用Update得到错误,或者发生任何事情 请帮帮我,我快疯了 public partial class Form3 : Form { //instance fields private export2Excel export2XLS; private DataSet _dataSet; pr
public partial class Form3 : Form
{
//instance fields
private export2Excel export2XLS;
private DataSet _dataSet;
private BindingSource _bsrc;
private OleDbDataAdapter _dAdapter;
private OleDbCommandBuilder _cBuilder;
private DataTable _dTable;
private void button1_Click(object sender, EventArgs e)
{
//create the connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='C:\\Documents and Settings\\dorota\\Moje dokumenty\\Visual Studio
2010\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\artb.mdb'";
//create the database query
string query = "SELECT * FROM Samochody";
System.Data.DataSet DtSet = new System.Data.DataSet();
_dataSet = DtSet;
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
dAdapter.FillSchema(_dataSet, SchemaType.Source);
_dAdapter = dAdapter;
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(_dAdapter);
_cBuilder = cBuilder;
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
_dTable = dTable;
//fill the DataTable
_dAdapter.Fill(_dTable);
//_dAdapter.TableMappings.Add("Samochody", "Table");
_dAdapter.Fill(_dataSet);
// --------------------- to datagridview !
//BindingSource to sync DataTable and DataGridView
BindingSource _bsrc = new BindingSource();
//set the BindingSource DataSource
//bSource.DataSource = _dTable;
_bsrc.DataSource = _dTable;
//_bsrc = bSource;
//set the DataGridView DataSource
dataGridView1.DataSource = _bsrc;
}
}
这里…:
private void sqlsave_Click(object sender, EventArgs e)
{
//int i=_dAdapter.Update(_dTable);
_dAdapter.Update(_dataSet.Tables["Samochody"]);
//_dAdapter.Update(_dataSet,"Samochody");
}
//-----------------------------------------
嗯。我已为此更改了sqlsave方法
private void sqlsave_Click(object sender, EventArgs e)
{
try
{
//_dAdapter.Update(_dataSet.Tables["Samochody"]);
OleDbCommand oldb= _cBuilder.GetUpdateCommand();
int i=oldb.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show(i+" rows affected.");
//_dAdapter.Update(_dataSet,"Samochody");
}catch(OleDbException oldbex){
System.Windows.Forms.MessageBox.Show(oldbex.ToString());
}
现在我终于得到了比错误信息更丰富的信息
“System.InvalidOperationException”类型的未处理异常
在System.Data.dll中发生其他信息:ExecuteOnQuery
需要开放且可用的连接。连接的电流
该州已关闭
所以,让我改变一些东西,我会让你知道,如果这是它
//----------------
不,不,太快了,撑不了多久。现在,在试图保存时,我又遇到了一个异常,
Connection已打开,但调试时无法发布映像,因为我看到OleDbCommand类型为_commandText的对象
更新样本体集合项目=?,数据DxOzycji autem od=
等等。
我想这就是原因。我说得对吗?怎么办?您没有为OLEDB数据适配器提供连接。试着这样做: 该示例与您的代码不同,但它显示了新连接的声明并将其传递给OleDbDataAdapter 我找到了答案: 但更好的方法是使用拖放并从代码中学习 表单现在将有一个dataset实例,它是Adapter.Fill和.Update方法的引用点 简单又有效!:D
我在这里找到了:您正在混合和匹配不同的方法。ExecuteOnQuery需要打开连接-CommandBuilder不会为您提供此连接。您需要将Update命令放入DataAdapter并运行更新。您可能必须提供自己的更新方法。确保您的表有一个PrimaryKey索引,您说过要将Update命令放入DataAdapter。如果我更新了我的_cBuilder,它是这个类中的私有字段,并且由button1\u Click命令中的_dAdapter初始化,这还可以吗?连接已打开。在sqlsave_中单击我拥有:我拥有一个连接`OleDbCommand oldb=\u cBuilder.GetUpdateCommand;oldb.Connection.Open;int i=oldb.ExecuteNonQuery;System.Windows.Forms.MessageBox.Showi+受影响的行//_更新_数据集,Samochody;oldb.Connection.Close;}CatchOLEDBEException oldbex{System.Windows.Forms.MessageBox.Showoldbex.ToString;}`\u cBuilder与其他Ole对象一样存储在此类的私有字段中,请查看主代码的顶部。我希望如果我存储这个在其他单击方法中初始化的对象是可以的,我不必每次都创建新的适配器、BindingSource等,如果我想从同一Windows窗体的不同函数中影响相同的数据,是吗?
string connetionString = null;
OleDbConnection connection ;
OleDbDataAdapter oledbAdapter ;
OleDbCommandBuilder oledbCmdBuilder ;
DataSet ds = new DataSet();
int i = 0;
string sql = null;
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
connection = new OleDbConnection(connetionString);
sql = "select * from tblUsers";
try
{
connection.Open(); // your code must have like this
oledbAdapter = new OleDbDataAdapter(sql, connection);
oledbCmdBuilder = new OleDbCommandBuilder(oledbAdapter);
oledbAdapter.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
ds.Tables[0].Rows[i].ItemArray[2] = "neweamil@email.com";
}
oledbAdapter.Update(ds.Tables[0]);
connection.Close();
MessageBox.Show ("Email address updates !");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Select Data|View Datasources. Your dataset should be visible in the DataSources Window.
Drag a table to a (new) form. VS2005 will add a load of components and a few lines of code.