Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CommandBuilder vs Stored Proc for updating
Message
De
25/01/2010 16:07:44
 
 
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01445437
Message ID:
01445892
Vues:
26
>Please let me start my questions with the end statement of your message:
>
>> 3) The DAL Save method would iterate through the rows in each table and call methods to populate the Parameter collection and call the appropriate Stored Procs, all which has been mentioned in previous posts.
>
>1. Do you mean (looking at the above) to use DataAdapter.update() method against the "changes" dataset? (I didn't think you meant to "manually" iterate through the rows and call the stored procedure but want to be sure that I understood correctly)


No, I meant manually. It's what the DataAdapter.Update() method does behind the scenes anyway, so you're not incurring any extra overhead at all. Remember my scenario ... I'm passing an XML string that is NOT a DiffGram, consequently I've lost the row state ... DataAdapter.Update() will *not* work under this scenario. So you have to manually do the same thing that the .Update() method does ... with the proper methods in your DAL (some of which I've mentioned in other posts ... remember the SetAllParameters() method that I mentioned from my blog post?), this is largely already automated. When the DAL processes the dsChanged DataSet, it knows to use the PUT set of stored procs for each table (remember, the PUTs do both Updates and Inserts, depending on whether the PK < = 0). Similarly, when it process the dsDeleted DataSet, it knows it's going to be using the DELETE set of stored procs for each table.

>2. You said "put the XML back into the appropriate DataSets". I don't understand what you mean by "appropriate". But say, I want to - for learning purpose - to bypass the conversion of changes dataset to XML and then back to dataset but instead pass the changes dataset from UI to DAL and then in DAL use the DataAdapter.update(), would it work?

The "appropriate" DataSets being dsChanged from the ChangedXML & dsDeleted from the DeletedXML, as I showed in my example. If you were going to pass DataSet changes directly from the UI to the DAL (with no intervening XML), you'd then only need to pass one DataSet ... that being the result of doing a ds.GetChanges(). This would include all changes, including the Deleted rows. The reason I had to use two parameters when passing XML (one for deletions and one for all other changes) is because the dsChanged.GetXml() method does NOT return any XML for deleted rows.

~~Bonnie




>
>>>1. Why do you create strings ChangedXML and DeletedXML if you still pas the datasets (dsChanged and dsDeleted) to the Web Service SAVE method? (I presume that if I don't use web service I would call some SAVE method of my DAL, right?)
>>
>>Damn! I screwed up ... that's what I get for posting code off the top of my head without the benefit of morning caffeine having properly kicked in. Sorry Dmitry, the Save() method should have been like this:
>>
>>
>>MyWebService.Save(ChangedXML, DeletedXML);
>>
>>
>>>And I don't want to pass the entire dataset to my DAL (my dataset could be fairly large, e.g. table with 1000 rows).
>>
>>No, you wouldn't be doing this ... even if you didn't do it the XML way, you'd use the DataSet.GetChanges() method to only pass the changes. That part doesn't change at all.
>>
>>>2. What do you do with the data sets dsChanged and dsDeleted in the SAVE method of your web service (and supposedly in the DAL)? Conceptually?
>>
>>
>>1) The Web Service Save() method would put the XML back into the appropriate DataSets using the ReadXml() method of the DataSet. I use a FillWithXml() method, which could be a DataSet extension method (off the top of my head, watch out <g>):
>>
>>
>>public static void FillWithXml(this DataSet ds, string XML)
>>{
>>    StringReader sr = new StringReader(XML);
>>    ds.ReadXml(sr, XmlReadMode.InferSchema);
>>    ds.AcceptChanges();
>>}
>>
>>// since the above is an extension method for DataSets, you would use it like this server-side to "reconstitute" the DataSets:
>>MyDataSet dsChanged = new MyDataSet();
>>MyDataSet dsDeleted = new MyDataSet();
>>dsChanged.FillWithXml(ChangedXML);
>>dsDeleted.FillWithXml(DeletedXML);
>>
>>
>>2) Then you'd pass those DataSets to the DAL (or preferably the Biz) Save() method.
>>
>>3) The DAL Save method would iterate through the rows in each table and call methods to populate the Parameter collection and call the appropriate Stored Procs, all which has been mentioned in previous posts.
>>
>>Hope that helps a bit more.
>>
>>~~Bonnie
>>
>>
>>>First, thank you very much for the detailed explanation.
>>>
>>>I don't use web service but still would like to learn to do things the most efficient way (since I am sure in the future I will have different applications).
>>>
>>>And I don't want to pass the entire dataset to my DAL (my dataset could be fairly large, e.g. table with 1000 rows).
>>>
>>>A couple of things are not clear to me:
>>>
>>>1. Why do you create strings ChangedXML and DeletedXML if you still pas the datasets (dsChanged and dsDeleted) to the Web Service SAVE method? (I presume that if I don't use web service I would call some SAVE method of my DAL, right?)
>>>
>>>2. What do you do with the data sets dsChanged and dsDeleted in the SAVE method of your web service (and supposedly in the DAL)? Conceptually?
>>>
>>>Again, thank you.
>>>
>>>>It probably only matters if you're doing something a bit different. I'll show you what I mean with an example:
>>>>
>>>>I used web service methods to communicate with the backend. In doing so, the data that needed to update the database was passed as an XML string over the wire. In order to do this with the smallest XML footprint (IOW, not by using a diffgram), I proceeded in this manner:
>>>>
>>>>
>>>>DataSet dsChanged = ds.GetChanges();
>>>>DataSet dsDeleted = ds.GetChanges(DataRowState.Deleted);
>>>>
>>>>string ChangedXML = dsChanged.GetXml();
>>>>dsDeleted.RejectChanges(); // necessary, otherwise you will get no XML
>>>>string DeletedXML = dsDeleted.GetXml();
>>>>
>>>>MyWebService.Save(dsChanged, dsDeleted);
>>>>
>>>>
>>>>Now, because I'm passing XML in this manner, I've lost the row state. IOW, when these XML strings are re-serialized server-side into a ChangedDataSet and a DeletedDataSet, since this XML (done in this manner) contains no row state, neither will the new server-side DataSets. I rely on PKs < =0 to indicate an added row, everything else is an update. And obviously, everything contained in the DeletedDataSet will be deleted rows.
>>>>
>>>>The above methodology would not work with the DataAdapter.Update() method for obvious reasons.
>>>>
>>>>The only way to use the DataAdapter.Update() is to have passed data that contains the row state, which means passing the DataSet itself to your server-side Save() method. Going over a web service, I think that .NET will automatically serialize the DataSet into XML for you, but it's serialized into a DiffGram which has a much, *much* larger footprint than what I outlined above. At any rate, I don't recommend sending a DataSet in this manner over a web service. If you're not using a web service, then that's a different matter.
>>>>
>>>>Anyway, I hope that cleared it up a bit.
>>>>
>>>>~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform