I have a C# database-driven application with a multi-table Access database as the back end. I addition to the tables I have several views that reformat the tables fo开发者_运维技巧r ease of viewing. My understanding is that views are intended for data display only and should not be used to change data, especially when they contain foreign key bindings. I'm fine with this.
So let's say I have a Patrons table and a vPatrons view:
DataTable Patrons;
DataTable vPatrons;
I have a DataGridView that is bound to vPatrons and a bunch of combo boxes and text boxes bound to Patrons. When the user makes changes and hits a Save button I commit the changes to the Patrons table:
_bindingSource.EndEdit();
_tableAdapter.Update(Patrons);
So now my Patrons DataTable, Patrons database table and vPatrons database view are all updated. But I still need to update my vPatrons DataTable! The only way I can think of to do this is to re-fill the entire vPatrons DataTable, which seems horribly inefficient to me:
_tableAdapter.Fill(vPatrons);
Does anyone know a better way to keep in-memory tables and views synchronized?
I think you are overcomplicating things, really! I guess an answer now but it's not easy to really tell you the best solution without having seen the schema of your table and view. I do not know how different these objects are in terms of columns.
if you need to read data from a table and to display the same data in another way or view, I would rather load the data once from the database and as you do, store it in the DataTable patrons.
in this way you have your data available in the .NET side.
if you now need same data manipulated somehow for display in UI, I would work with a DataView in the .NET side which allows you to stay connected to the same datatable, you create it this way:
var myView = new DataView(patrons);
at this point the view is connected to the table, you can filter, sort, add expression columns and so on and it always stays connected with the data.
if you think that your view in the database is too complicated and you do not want to recreate such view logic in the .NET application, then is also fine, you should decide if you load the table or the view, consider that you can also only load the view in a table and once you have your IDs you can send updates to the database with specific update statements, not need to only work with the whole tableAdapter save.
either way, up to you but I would really avoid to have to keep the two objects in .NET application synchronized... (they are both DataTables actually even if one of them you fill with a view from access).
精彩评论