Dataset/DataAdapter and updating changes to the database..
-
Hi. I'm quite new to using DataSets/DataAdapters. At first, I tried to populate my DataSet (which consists of several tables) with one select query, like so:
var cmd =
new SqlCommand("SELECT * FROM TableOne;SELECT * FROM TableTwo", con);
myDataAdapter= new SqlDataAdapter(cmd);myDataAdapter.Fill(ds);
I then proceed to set the primary key for TableTwo in the dataset and instantiate a SqlCommandBuilder with the Adapter as parameter. When I eventually call myDataAdapter.Update(ds.Tables["TableTwo"]); the adapter gets confused and complains that it can't find a column from TableOne in TableTwo. I googled around and read that you can fix this problem by using seperate DataAdapters for each table in the DataSet. I tried this, creating a new SqlCommand for each adapter with only one select query in each, but calling the second myDataAdapter.Fill overwrites the contents of the DataSet completely! Of course I could use seperate DataSets for each table but that seems like the incorrect solution. I've googled for tutorials and examples but all of them are too simple to help me (never more than 1 table in the DataSet, and some other issues as well). Basically, I'd appreciate some advice on how to approach this problem - is it doable with one DataAdapter? if so, how do I get the DataAdapter to understand my DataSet (which seems to be the problem here..)? Otherwise, how do I use more than one DataAdapter to populate a single DataSet? Thanks in advance for any help...
-
Hi. I'm quite new to using DataSets/DataAdapters. At first, I tried to populate my DataSet (which consists of several tables) with one select query, like so:
var cmd =
new SqlCommand("SELECT * FROM TableOne;SELECT * FROM TableTwo", con);
myDataAdapter= new SqlDataAdapter(cmd);myDataAdapter.Fill(ds);
I then proceed to set the primary key for TableTwo in the dataset and instantiate a SqlCommandBuilder with the Adapter as parameter. When I eventually call myDataAdapter.Update(ds.Tables["TableTwo"]); the adapter gets confused and complains that it can't find a column from TableOne in TableTwo. I googled around and read that you can fix this problem by using seperate DataAdapters for each table in the DataSet. I tried this, creating a new SqlCommand for each adapter with only one select query in each, but calling the second myDataAdapter.Fill overwrites the contents of the DataSet completely! Of course I could use seperate DataSets for each table but that seems like the incorrect solution. I've googled for tutorials and examples but all of them are too simple to help me (never more than 1 table in the DataSet, and some other issues as well). Basically, I'd appreciate some advice on how to approach this problem - is it doable with one DataAdapter? if so, how do I get the DataAdapter to understand my DataSet (which seems to be the problem here..)? Otherwise, how do I use more than one DataAdapter to populate a single DataSet? Thanks in advance for any help...