DataSet TableAdapter 填充方法
我有一个带有两个 TableAdapter(一对多关系)的数据集,是使用 Visual Studio 2010 的配置向导创建的。
我调用外部源并用结果填充字典。这些结果应该是数据库中的所有条目。为了同步数据库,我不想只清除所有表,然后重新填充它们,就像删除表并使用 sql 中的新数据创建它们一样。
是否有一种可能使用 TableAdapter.Fill() 方法的干净方法,或者我是否必须逐行循环遍历两个表并决定它是保留还是被删除,然后添加新条目?使字典中的数据成为具有数据集的两个表中的唯一数据的最佳方法是什么?
I have a DataSet with two TableAdapters (1 to many relationship) that was created using visual studio 2010's Configuration Wizard.
I make a call to an external source and populate a Dictionary with the results. These results should be all of the entries in the database. To synchronize the DB I don't want to just clear all of the tables and then repopulate them like dropping the tables and creating them with new data in sql.
Is there a clean way possibly using the TableAdapter.Fill() method or do I have to loop through the two tables row by row and decide if it stay or gets deleted and then add the new entries? What is the best approach to make the data that is in the dictionary be the only data in my two tables with the DataSet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个问题:如果是同一个数据库,为什么有 2 个具有相同信息的表?
对于手头的问题:这很大程度上取决于尺寸。如果表不大,则使用事务,清除表(DELETE * FROM TABLE 或其他)并再次将数据写入其中。
另一方面,如果表很大,问题是:你能将所有这些加载到你的字典中吗?
当然,您必须问自己不一致的数据会发生什么(另一个用户/应用程序更改了您的字典中的数据)。
如果这需要很长时间,您可以记住您对数据所做的操作 - 这意味着:标记更改的数据并记住已删除的键和新插入的行,并据此进行更新。
两者都可以通过记住填充的数据表并将其用作支持字段或通过实现您自己的机制来实现。
无论如何,我建议思考这个问题:你真的需要字典吗?为什么不查询数据库来获取数据呢?或者只缓存一部分数据以便快速访问?
PS:DataAdapter 上的更新方法将完成所有工作(更改更改、删除删除并插入新数据行,但它将更新 DataTable/Set,因此这只有效一次)
First Question: if it's the same DB why do you have 2 tables with the same information?
To the question at hand: that largley depend on the sizes. If the tables are not big then use a transaction, clear the table (DELETE * FROM TABLE or whatever) and write your data in there again.
If the tables are big on the other hand the question is: can you load all this into your dictionary?
Of course you have to ask yourself what happens to inconsistent data (another user/app changed the data while you had it in your dictionary).
If this takes to long you could remember what you did to the data - that means: flag the changed data and remember the deleted keys and new inserted rows and make your updates based on that.
Both can be achieved by remembering the Filled DataTable and use this as backing field or by implementing your own mechanisms.
In any way I would recommend think on the problem: do you really need the dictionary? Why not make queries against the database to get the data? Or only cache a part of the data for quick access?
PS: the update method on you DataAdapter will do all the work (changing the changed, removing the deleted and inserting the new datarows but it will update the DataTable/Set so this will only work once)
重新填充整个表可能比遍历并决定保留/保留哪些记录更快。难道不能通过sql语句来决定是否删除记录吗? (从活跃 = false 的表中删除)如果您希望它们保留在数据库中但不在数据集中(从活跃 = true 的表中选择 *)
您可以有一个日期字段并选择自该日期以来添加的所有记录您迟到“汇集”数据库(从表中选择 *,其中 active = true 且添加日期 > #12:30#)
It could be that it is quicker to repopulate the entire table than to itterate through and decide what record go / stay. Could you not do the process of deciding if a records is deleteed via an sql statement ? (Delete from table where active = false) if you want them to stay in the database but not in the dataset (select * from table where active = true)
You could have a date field and select all records that have been added since the date you late 'pooled' the database (select * from table where active = true and date-added > #12:30#)