2.) DataSet dsCustomers =new DataSet();
3.) SqlDataAdapter daCustomers =new SqlDataAdapter("select CustomerID, CompanyName from Customers", conn);
4.) SqlCommandBuilder cmdBldr =new SqlCommandBuilder(daCustomers);
5.) daCustomers.Fill(dsCustomers, "Customers");
6.) daCustomers.Update(dsCustomers, "Customers");
Question:
step 5 is we fill data into dataset called dsCustomers, and a table called Customers in dataset??
step 6 is we update our database's table called Customers based on dsCustomers dataset's Customers table??
what is the major function for "SqlCommandBuilder"? if i skipped step 4, is it dsCustomers content any data or still empty after step 5?
what about we want to update our data which is content in our dataset (dsCustomers) after step 5?
Thank You!
Rgs,
FslimAh ADO.NET. I admit, it took me forever to figure out how to build commands properly and use DataSets.
Personally, I find it better to write your own commands, if they are needed. ie, I only make insert/update/delete commands for the adapter if I know my page is going to need those commands.
Step 5. You are filling Dataset dsCustomers with your select command. In this case it is selecting ONLY the CustomerID and CompanyName columns from your Customers Table in the Database. It is taking that information and putting it into a DataTable named "Customers" (cuz daCustomers.Fill(dsCustomers,"Customers"); ) in your Dataset.
Step 6. You are telling your DataAdapter to run update on DataTable "Customers" in Dataset dsCustomers. What this does it look at each row in the DataTable and runs the proper command. ie, if there is an inserted row it uses the Insert command of the DataAdapter to insert it into the source Database. Deleted rows it uses the Delete command to delete from the Database.
The SqlCommandBuilder is making the Insert/Update/Delete commands for your SqlDataAdapter. Without it you would not be able to run update since your Adapter wouldn't know how to process rows that have been inserted, updated, or deleted.
To answer your question, without Step 4 Step 5 works just fine (since it depends on the DataAdapters SELECT statement, not any of the others), but Step 6 would fail.
Instead of using the Command Builder you can write your own commands however. If you need help with that I would be more than happy to help. I use OleDB myself, but the SQL statements are pretty much the same, I believe.
Joshua
A DataSet has 4 commands , a SELECT, DELETE, INSERT and UPDATE. What SqlCommandBuilder does is creates update, delete and insert commands from examining your select command. Typically it's probably a better idea to create your own command objects,a s you can then customise the queries. so if you skipped step 4 then your DataSet would still contain data, but you wouldn't be able to update any changes back to the database.
As you have correctly stated Step 5 fills a table called Customer and step 6 updates any changes you have made to the DataSet back to the database (You haven't made any changes here, so it won't have anything to update)
fslim||shady--
Regarding this...
fslim_shady wrote:
step 5 is we fill data into dataset called dsCustomers, and a table called Customers in dataset?
...that is, (more or less), correct.
Regarding this...
fslim_shady wrote:
step 6 is we update our database's table called Customers based on dsCustomers dataset's Customers table??
...that is right; however, in the code sample that you provided, there are no changes made to the DataSet between Line5 and Line6, so there are no changes to be saved.
Regarding this...
fslim_shady wrote:
what is the major function for "SqlCommandBuilder"? if i skipped step 4, is it dsCustomers content any data or still empty after step 5?
...the SqlCommandBuilder "Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated SQL Server database". See "SqlCommandBuilder" athttp://MSDN.Microsoft.com for more details.
Regarding this...
fslim_shady wrote:
what about we want to update our data which is content in our dataset (dsCustomers) after step 5?
...note that you need to change the DataSet, somewhere between Line5 and Line6, before you call the Update() method, in order to have changes appear.
There are many different ways to handle data access with SQL Sever. If you want to see another way to do it, you can find a working online example on how to use a SQL Sever Database with .NET, with all of the source code, at this location...
http://www.WebLogicArts.com/DemoSqlServerDatabase01.aspx
HTH.
0 comments:
Post a Comment