Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Updating DataSet with Multiple Tables
Message
From
16/11/2004 16:13:06
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 1.1
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Miscellaneous
Thread ID:
00961815
Message ID:
00961926
Views:
5
Mike,

Unfortunately the CommandBuilder has several limitations, one of which is that it only works on single table DataSets. So, basically, you'll have to "roll your own" Command Builder. You can probably utiize the DataAdapter's .SelectCommand property to figure out which tables you've populated and then generate the appropriate Insert, Delete, etc. commands. You'd also have to use the optional DataTable parameter in the DataAdapter.Update() method to only update one table at a time. Hmmmm ... you'd probably also need to know about relationships (if you've set them up in your DataSet) so that you update tables in the correct order. Probably want to wrap it all up in a Transaction too.

I haven't done any of this myself, so I'm just laying out a general outline of what you might try. Most of my DataAccess stuff is not generic, but specific to the particular DataSets and business logic, so I'm just blue-skying this. <g>

~~Bonnie



>Hi Bonnie,
>
>Here is the code for SaveDataSet():
>
>
>public override int SaveDataSet(DataSet ds)
>{
>	SqlDataAdapter DataAdapter;
>	
>	if (ds is CDataSet)
>	{
>		/// If this is a custom DataSet, get a reference to the
>		/// DataSet's data adapter and store it in the DataAdapter variable
>		CDataSet dsSql = (CDataSet)ds;
>		DataAdapter = (SqlDataAdapter)dsSql.DataAdapter;
>	}
>	else
>	{
>		/// If this is NOT a Custom DataSet, create a new Data Adapter
>		DataAdapter = new SqlDataAdapter();
>	}
>	SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataAdapter);
>	CommandBuilder.QuotePrefix = "[";
>	CommandBuilder.QuoteSuffix = "]";
>
>	DataAdapter.DeleteCommand = CommandBuilder.GetDeleteCommand();
>	DataAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand();
>	DataAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
>
>
>	// Update the data in the DataSet
>	int RowsUpdated = DataAdapter.Update(ds, ds.Tables[0].ToString());
>			
>	return RowsUpdated;
>}
>
>
>>Mike,
>>
>>I have Kevin's book, but I haven't downloaded his sample code. How is the current SaveDataSet() method currently coded? Does it not include a way to work with a multi-table DataSet? I've not written any "generic" data access class to do this sort of thing (all my data access classes are specific to what's being updated), so looking at the code for the sample SaveDataSet() method that you're already using may give me some ideas of how to "generic-ize" it. <g>
>>
>>~~Bonnie
>>
>>
>>
>>>I am trying to enhance the WebForms example application from Chapter 10 of Kevin McNeish's book '.Net for Visual FoxPro Developers'.
>>>
>>>On the OrderEdit form, I added Edit buttons to the grid grdOrderItems. This worked ok. Then, I tried to join the Products table with the OrderItems table, in the DataSet, to show the ProductName rather than the ProductID.
>>>
>>>The ProductName shows ok, but when I try to do any edits, I get an error that the CommandBuilder can't build commands for DataSets with multiple tables; you have to define your own commands manually. However, I was trying to use Kevin's business objects, from Chapter 8, where there is a generic SaveDataSet() method in the DataAccessSql data object in the Data.cs file, where commands are built on-the-fly for the DataSet passed in.
>>>
>>>Is there any way to keep using business objects with DataSets with multiple tables? How would I need to code my DataAdapter DeleteCommand, etc. to be generic (to work with any DataSet passed in)?
>>>
>>>Thanks
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform