TableAdapter.Update不提交 [英] TableAdapter.Update doesn't commit
问题描述
我遇到了tableadapter.update无法写入本地访问db(即MS Access 2007 mdb)的问题.我正在使用C#在VS2010中工作.我正在使用强类型数据集(XSD).
我的应用程序将数据绘制到本地数据库中.当我说本地时,是指我将Access DB复制到项目中,并且app.config中的数据源反映了| Data Directory |.作为它的连接字符串的一部分.
我可以毫不费力地添加行,但是如果我尝试修改现有行中的值,则似乎无法在基础数据库中得到它.我有一个打印/不打印字段(在访问mdb中定义为是/否"字段),我希望我的用户能够切换该字段.但是,以下方法似乎并未更新基础表,也不会引发任何错误:
I have an issue with tableadapter.update not writing to a local access db, which is MS access 2007 mdb. I am working in VS2010 using c#. I am using a strongly typed dataset (XSD).
My application draws data into a local db. When I say local, I mean in the sense that I copied the Access DB to the project and the datasource in app.config reflects |Data Directory| as part of it''s connection string.
I can add rows with out any difficulty, but if I attempt to modify values in an existing row, I don''t seem to get that in the underlying database. I have a print/noprint field (defined in access mdb as a Yes/No field) which I want my user to be able to toggle. Yet, the following method doesn''t seem to update the underlying table, nor does it throw any errors:
private void togglePrintToolStripMenuItem_Click(object sender, EventArgs e)
{
dsH3TableAdapters.lblSourceTableAdapter taLblSource = new dsH3TableAdapters.lblSourceTableAdapter();
taLblSource.FillAll(this.dsH3.lblSource);
dsH3.lblSourceDataTable dtLabels = taLblSource.GetDataAll();
foreach (dsH3.lblSourceRow rowLabels in dtLabels)
{
bool printingState = rowLabels.bPrintLabel;
rowLabels.BeginEdit();
rowLabels.bPrintLabel = !printingState;
rowLabels.EndEdit();
rowLabels.AcceptChanges();
taLblSource.Update(this.dsH3.lblSource);
}
taLblSource.FillAll(this.dsH3.lblSource);
}
我可能不完全了解如何使用表适配器,数据表和数据行,但是最近几天我做了很多搜索,很多人似乎都遇到了这个问题,没有明确定义的解决方案.
我尝试了各种beginedit,endedit,acceptchanges,忽略了这些等等,但都无济于事.我可以通过使用rowLabels.Order_No和rowLabels.LineNo(它们是表的主键字段,并且在XSD中定义为此类)在xsd中调用更新查询来更新行,但这是一个比较耗时的操作,并且如果可以的话,我宁愿进行上述工作.
我认为使用本地数据库不是问题,我已阅读
It is possible that I don''t fully understand how to use tableadapters and datatables and datarows, but I have done a lot of searching the last few days and a lot of people seem to have this problem with no clearly defined solution.
I''ve tried a variety of beginedit, endedit, acceptchanges, omitting these, etc, to no avail. I can update the rows by calling an update query in the xsd using rowLabels.Order_No and rowLabels.LineNo (which are the primary key fields for the table and defined as such in the XSD), but that is a more time-consuming operation and I would rather make the above work, if I can.
I don''t think this is an issue with using the local database, I have read the article at MSDN about that, and the fact is my methods to add rows works fine, and all methods that call a query in the xsd work fine. It is just this direct update of the row that doesn''t seem to work.
I read a post saying this might be a problem if some fields in the db allow nulls, I do allow nulls in some fields.
Appreciate any help. Thank you.
推荐答案
这只是一个猜测,因为自从我使用TableAdapters和DataSet以来已经很长时间了,但是不必设置DataRow.RowState = DataRowState.Modified;
在TableAdapter意识到发生了什么之前?
It''s just a guess as it''s a long time since I used TableAdapters and DataSets, but don''t you have to set DataRow.RowState = DataRowState.Modified;
before the TableAdapter realises what''s going on?
我意识到这是一个非常老的问题,但这是以后的参考答案:请勿在之前调用AcceptCancel()更新()!它将标记该行为未更改,并且显然不会被更新.我同意MS文档对此有些困惑.
可变
I realize this is a very old question, but here is the answer for future reference: do NOT call AcceptCancel() before Update()! It will mark the row as Unchanged, and obviously it will not be updated. I agree that the MS documentation is somewhat confusing on this.
Vasile
我有一个类似的问题,但是,我也无法向数据库添加行.我可以向数据集的表添加一行,但是tableadapter.update不会将更改提交到数据库.同时,tableadapter.insert确实可以工作,即在数据库中出现新行.
I have a similar issue, however, I also cannot add rows to database. I can add a row to dataset''s table but tableadapter.update does not commit the changes to the database. At the same time tableadapter.insert does work though, i.e. new rows appear in database.
这篇关于TableAdapter.Update不提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!