about understanding

T

Tony

Hello!

I'm reading a book called "Programming Microsoft ADO.NET 2.0 Core Reference
by David Sceppa"
And there is something that I don't really understand here.
This is copied from the book first a piece of code then below I have some
text also copied from the book.

string strConn, strSQL;
strConn = "@Data Source=.\SQLEXPRESS; Initial Catalog=Northwind;Integrated
Security";
strSQL = "select CustomerID, CompanyName from Customers";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter (strSQL, strConn);
da.Fill(ds);
foreach(DataRow row in ds.Tables[0].Rows)
Console .WriteLine("{0} - {1}", row["CustomerID"], row["CompanyName"]);

Once this call completes, there is no connection between the two objects.
The DataSet does not maintain a reference, internally or externally to the
SqlDataAdapter and the SqlDataAdapter does not maintain a reference to the
DataSet. Also the DataSet contain no information indicating where the data
originated - no connection string, no table name. The DataSet does contain
the column names by default but these column names do not need to match the
names of the columns in your database table. Thus you can pass DataSet
objects from your middle-server to your client application without
divulging any information about the location or structure of your database.

Now what seems strange to me is that the text says that the SqlDataAdapter
doesn't have a reference to the DataSet.
How is it then possible for the SqlDataAdapter to perform update in the
database for the changes that have been made in the dataset if the
SqlDataAdapter doesn't have a reference to the dataset ?
Here is how SqlDataAdapter will perform update in the dataset da.Update(ds);

//Tony
 
J

Jeff Johnson

doesn't have a reference to the DataSet.
How is it then possible for the SqlDataAdapter to perform update in the
database for the changes that have been made in the dataset if the
SqlDataAdapter doesn't have a reference to the dataset ?
Here is how SqlDataAdapter will perform update in the dataset
da.Update(ds);

I don't use this method much, but I believe the answer is that the DataSet
contains information on which of its rows have changed. The
DataAdapter.Update() method then scans every row looking for those marked
"changed" and then passes the information in that row to the UpdateCommand
or whatever it's called. So it's true that there is no connection between
the two objects.
 
T

Tony

Jeff Johnson said:
I don't use this method much, but I believe the answer is that the DataSet
contains information on which of its rows have changed. The
DataAdapter.Update() method then scans every row looking for those marked
"changed" and then passes the information in that row to the UpdateCommand
or whatever it's called. So it's true that there is no connection between
the two objects.
I see what you mean but as you might know every DataRow is located within a
DataTable that exist in a DataSet so
the SqlDataAdapter can't access a DataRow without also reference the
DataTable and DataSet.

//Tony
 
J

Jeff Johnson

I see what you mean but as you might know every DataRow is located within
a DataTable that exist in a DataSet so
the SqlDataAdapter can't access a DataRow without also reference the
DataTable and DataSet.

So? You pass a DataTable or DataSet to the DataAdapter.Update() method,
don't you? There's no "reference" there; it's a parameter! What I'm saying
is I think your original text is correct in that the DataAdapter maintains
no "long-term knowledge" of any DataSet or DataTable it has interacted with,
and vice-versa.
 
T

Tony

Jeff Johnson said:
So? You pass a DataTable or DataSet to the DataAdapter.Update() method,
don't you? There's no "reference" there; it's a parameter! What I'm saying
is I think your original text is correct in that the DataAdapter maintains
no "long-term knowledge" of any DataSet or DataTable it has interacted
with, and vice-versa.

Yes that seems to be correct because as the text says there is no persistent
relationship between the DataSet and the DataAdapter, which is why I have to
pass a reference to the Dataset into the DataAdapter's Update method.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top