ADO.NET Update奇怪问题
有一段C#程序,在第二个update处始终会有DBConcurrencyException。我的目的是想用DataView的AddNew()先新增一行,然后保存到数据库中。接着马上再对这个DataRowView做些修改,再保存到数据库。但这一步会出错。实在不清楚为什么??请高手指点原因!public void test()
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.mdb";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from table", connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "table");
DataTable table = dataSet.Tables["table"];
DataView view = new DataView(table);
view.AllowNew = true;
view.AllowEdit = true;
view.AllowDelete = true;
DataRowView row = view.AddNew();
row.BeginEdit();
row["ID"] = Guid.NewGuid().ToString();
row.EndEdit();
adapter.Update(table);
row.BeginEdit();
row["ABC"] = "TEST";
row.EndEdit();
adapter.Update(table); //DBConcurrencyException:Update...
connection.Close();
}
--------------------编程问答-------------------- 友情帮顶 --------------------编程问答-------------------- 报的什么错 --------------------编程问答-------------------- table.AcceptChanges();放到你第一个update后面. --------------------编程问答-------------------- 把"table.AcceptChanges();"这条语句分别放在两条adapter.Update(table);前面就行了。 --------------------编程问答-------------------- to youyouke:
你那种方法肯定不行,数据库根本就不会保存更新的。
to lovehongyun:
table.AcceptChanges()会在update中自动被调用的。
问题还没有解决!大家快快来帮忙!多谢啦! --------------------编程问答--------------------
你试了吗?
update后调用AcceptChanges(); --------------------编程问答-------------------- string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.mdb";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from table", connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "table");
//DataTable table = dataSet.Tables["table"];
DataRow row= DataSet.Tables[0].NewRow();
row.BeginEdit();
row["ID"] = Guid.NewGuid().ToString();
row.EndEdit();
Ds.Tables[0].Rows.Add(row);
adapter.Update(table);
dataSet.AcceptChanges(); --------------------编程问答-------------------- 给你一个参考
public static int UpdateDataset(string ConnectionSql, DataSet dataset, string tablename)
{
int ii;
string updatesql = "select top 0 * from " + tablename;
using (SqlConnection conn = new SqlConnection(ConnectionSql))
{
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
SqlDataAdapter da = new SqlDataAdapter(updatesql, conn);
da.SelectCommand.Transaction = trans;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, tablename);
ds = dataset.Copy();
try
{
ii = da.Update(ds, tablename);
trans.Commit();
}
catch (System.Data.SqlClient.SqlException e)
{
trans.Rollback();
throw new Exception(e.Message);
}
finally
{
da.Dispose();
cb.Dispose();
ds.Dispose();
}
}
return ii;
}
调用方法
if (Conn.UpdateDataset(Conn.GetDb, Ds, "Materialprice") >= 0)
{
Ds.AcceptChanges();
MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} --------------------编程问答-------------------- d --------------------编程问答-------------------- 你能确定第二次保存时更改的还是刚才加的一行吗?还是有一个新行? --------------------编程问答-------------------- 我在电脑上执行楼主的代码,没有报错。我连的sql server 2000.
private void button5_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=pubs;server=(local)";
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from test1", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "table");
DataTable table = dataSet.Tables["table"];
DataView view = new DataView(table);
view.AllowNew = true;
view.AllowEdit = true;
view.AllowDelete = true;
DataRowView row = view.AddNew();
row.BeginEdit();
row[0] = Guid.NewGuid().ToString();
row.EndEdit();
adapter.Update(table);
row.BeginEdit();
row[1] = "TEST";
row.EndEdit();
adapter.Update(table); //DBConcurrencyException:Update...
conn.Close();
}
运行程序,点了三次按钮也没爆粗o
74549b02-26c6-47d3-bc82-5719fb2ae70e TEST
d4266e2d-fd1f-45da-b06e-158a9861d0ef TEST
fc1e3055-69a1-4459-a16c-8a610ffd827f TEST
--------------------编程问答-------------------- 试过你说的方法了。依然报错
--------------------编程问答-------------------- up --------------------编程问答-------------------- row["ID"] = Guid.NewGuid().ToString();
长度对不对?? --------------------编程问答-------------------- 长度没问题 --------------------编程问答-------------------- DataRowView row = view.AddNew();
下面那句话也加上这个试试 --------------------编程问答-------------------- 我的目的就是修改刚刚增加的那一行。
我试过,修改其他行就没有问题,特别奇怪! --------------------编程问答--------------------
看看其他地方,是不是数据库没设置好? --------------------编程问答--------------------
数据库应该是连接对了吧。我十分怀疑是不是OleDB连接Access数据库的问题。
哪位如果实验,可以先用OleDB连接Access数据库试试。这个问题十分奇怪,请用过OleDB的高手指点
--------------------编程问答-------------------- jf --------------------编程问答-------------------- 我觉得4楼的做法可以啊,你试过没lz --------------------编程问答--------------------
试过了。不好意思,我要说的是4楼的方法是不对的,因为那样做的话,后面的update就无法保存任何更新了。具体 原理大家可以参考msdn上关于ado.net的讲解。 --------------------编程问答-------------------- 问题依旧,实在太奇怪了
不知道有没有ADO.NET方面的专家帮忙解决一下,谢谢了! --------------------编程问答-------------------- 楼主,sql server2000 和 Access2003 我都试过了。我同时开着Access文件的,也没有冲突啊。
private void button5_Click(object sender, EventArgs e)
{
updateAccess();
}
private void updateAccess()
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.mdb";
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from test1", conn);
conn.Open();
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "table");
DataTable table = dataSet.Tables["table"];
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = table;
DataView view = new DataView(table);
view.AllowNew = true;
view.AllowEdit = true;
view.AllowDelete = true;
DataRowView row = view.AddNew();
row.BeginEdit();
row[0] = Guid.NewGuid().ToString();
row.EndEdit();
adapter.Update(table);
row.BeginEdit();
row[1] = "TEST";
row.EndEdit();
adapter.Update(table); //DBConcurrencyException:Update...
conn.Close();
}
private void updateSQL()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=pubs;server=(local)";
SqlDataAdapter adapter = new SqlDataAdapter("select * from test1", conn);
conn.Open();
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "table");
DataTable table = dataSet.Tables["table"];
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = table;
DataView view = new DataView(table);
view.AllowNew = true;
view.AllowEdit = true;
view.AllowDelete = true;
DataRowView row = view.AddNew();
row.BeginEdit();
row[0] = Guid.NewGuid().ToString();
row.EndEdit();
adapter.Update(table);
row.BeginEdit();
row[1] = "TEST";
row.EndEdit();
adapter.Update(table); //DBConcurrencyException:Update...
conn.Close();
}
Access中的test1表,就两字段tid,tname,都是文本类型。tid是主键。
连点了5次:
tid tname
361e08bb-adf3-421e-90af-af2b73b23d90 TEST
6727bf4c-9ae7-4217-949a-51f4bbbea5e8 TEST
75b5cd06-6185-47af-8abf-90d34fc454b9 TEST
8925924e-f553-4db1-ba99-108f81a5bb90 TEST
ff46564d-c0a4-45aa-9792-2c24f5d3df3d TEST
这上面怎么贴图啊。
补充:.NET技术 , C#