the problem:
I'm trying to get information from one database table and load it into a dataset using OleDbDataAdapters. I can do that part successfully. I think need to take that DataSet and insert the WHOLE thing into a different database. How do i get the 2nd DataAdapter to get the data from the dataset?
Eventually to add to my frustration, i'll need to merge 2 datasets and do the same thing?
Any suggestion as to how to accomplish this?
Thanks in advance,
Scott
jmcilhinney
July 27th, 2005, 03:14 AM
First off, set the AcceptChangesDuringFill property of the DataAdapter to False. This will leave all retrieved rows marked as Added when you call Fill. Now create an appropriate Command object with the required SQL statement and a connection to the correct database and assign it to the InsertCommand property of the DataAdapter. Then you just call Update on the adapter and pass the DataSet (if there is only one table) or the appropriate DataTable (if there are more than one). Because the rows are still marked as Added, they will be inserted into the new database.
sonicsqwirl
July 27th, 2005, 03:55 AM
Ok I think I've done what you suggest, and it builds fine... and runs... but no data is written to the second dataset...here is the code
If you need to transfer big amount of data you should use DataReader instead of DataSet.
sonicsqwirl
July 27th, 2005, 03:57 AM
how would a dataReader be more advantageous... this program will be transfering about 1000 records every time its ran... so i need pretty quick and efficient.
thanks
torrud
July 27th, 2005, 04:10 AM
1.000 records handles a dataset without any problems. If you had 1.000.000 records then I would prefer DataReader and DataWriter.
sonicsqwirl
July 27th, 2005, 04:15 AM
ok so DataSets will work with the data, but why is the data not being updated in the second database (xlConn3)?
thanks
jmcilhinney
July 27th, 2005, 04:17 AM
Your code looks fairly good. I'd say that your going to have to do some debugging. Confirm that there are new rows in the table by calling GetChanges. Use the return value of Update to see how many rows were affected by the call.
Forget the calls to Open the Connections. They are not needed. When calling Fill or Update, the DataAdapter will open and close the connection if it is not already open. The only reason to do it yourself is if you are doing multiple Fills or Updates over the same connection. Opening the connection yourself saves it being closed and reopened between calls.
As for a DataReader, using a DataReader is faster than using a DataAdapter. The problem is that you have to write the code to populate the DataTable yourself, and you will have to use a DataTable or else you'll have to insert each row into the new database individually. This would more than offset any speed gain from using a DataReader.
On the point of DataSets, it seems that everyone loves them. A DataSet is a way of grouping multiple DataTables and the DataRelations between them. If there are no relations, and especially if there is only one table, just use a DataTable directly .
jmcilhinney
July 27th, 2005, 04:26 AM
1.000 records handles a dataset without any problems. If you had 1.000.000 records then I would prefer DataReader and DataWriter.I just repped you because I thought you were a genius for suggesting the DataWriter that I've never heard of. Then I had a look in the help to read about it and I discover that the reason I haven't heard of it is because it doesn't exist. Gimme that rep back! :)
sonicsqwirl
July 27th, 2005, 04:27 AM
when i use
xlDataGrid3.DataSource=ds.GetChanges(DataRowState.Added);
the datagrid will show the rows that needed to be added... so its gotta be something with my insert command to the 2nd database (xlconn3) aye?
thanks
jmcilhinney
July 27th, 2005, 04:29 AM
when i use
xlDataGrid3.DataSource=ds.GetChanges(DataRowState.Added);
the datagrid will show the rows that needed to be added... so its gotta be something with my insert command to the 2nd database (xlconn3) aye?
thanksIt would appear so. Without knowing the database structure the code looks good to me. What value does Update return?
sonicsqwirl
July 27th, 2005, 04:32 AM
i'm kind of new to this... what does
"What value does Update return?" mean?
sorry for the ignorance... my first C# project and it's kinda a big one
thanks
sonicsqwirl
July 27th, 2005, 04:34 AM
the database structure i'm using for the test databases
db1 has a table named NamesTest, with ID, FirstName, LastName as field names
db3 is exactly the same just with no data at all in the database... db1 has 10 records in the NamesTest table.
jmcilhinney
July 27th, 2005, 04:41 AM
The Update method of the DataAdapter is actually a function, i.e. it returns a value, specifically the number of rows that were affected, i.e. deleted or inserted or updated, by the call. If there are rows to be affected and it returns zero then that is odd. With Added rows and a valid InsertCommand, I would think that the Call to Update should return a value greater than zero or throw an exception. Anything else is unacceptable, **** it! :)
Maybe you could try the same thing with two tables in the same database. If that works but it doesn't between databases then that must be the issue. I've never actually tried this myself but I can't see why there should be a problem.
sonicsqwirl
July 27th, 2005, 04:45 AM
ok i feel really dumb now, but how do i know what number Update returns?
sorry again
sonicsqwirl
July 27th, 2005, 04:47 AM
ok figured that part out... and it is 0 as far as i can tell
int count = sda.Update(ds, "NamesTest");
int count = 0 when you mouse over it in debug mode
sonicsqwirl
July 27th, 2005, 04:48 AM
hee hee... scratch that... it equals 11
jmcilhinney
July 27th, 2005, 04:56 AM
It's just a function returning a value like any other, so you simply assign it to a variableint rowsAffected = myAdapter.Update(myTable);or you can display it immediatelyMessageBox.Show("Rows affected: " + myAdapter.Update(myTable).ToString());
sonicsqwirl
July 27th, 2005, 04:58 AM
Maybe you could try the same thing with two tables in the same database. If that works but it doesn't between databases then that must be the issue. I've never actually tried this myself but I can't see why there should be a problem.
I tried to transfer the same data with the same insert statement to an identical table named NamesTest2 in the same database and the data transfered flawlessly..... i'm confused now....
thanks
jmcilhinney
July 27th, 2005, 05:04 AM
Well the number returned by Update is the number of rows affected, so if it returns 11 then that should mean that it's inserted 11 rows. If it is genuinely working within the same database but not between databases then I am confused. Like I said, never done it myself but I don't see why it shouldn't be possible.
The other option would be to create a DataTable and get the schema from the second database using FillSchema. You could then get the data from the first database with a DataReader and create the new rows in the DataTable yourself. Then you should be able to Update the new data because it should know that the table is from that same database. Seems like a workaround for a problem that shouldn't exist but necessity and invention.
sonicsqwirl
July 27th, 2005, 05:07 AM
well i appreciate the help... i'lll prolly asking more questions in the future... this project seems to keep getting bigger.
sda.InsertCommand = insCmd;
xlConn.Open();
xlConn3.Open();
sda.Update(ds, "NamesTest");This is wrong code! And hence will not do anything that you are trying to achieve. The correct and easier way of using the connection, datareader and dataset object in this particular scenario follows this order:
1. Open connection with database 1
2. Retrieve data into DataSet from this datasource
3. Close connection with database 1
and then
1. Open connection with database 2
2. Make a new copy of the earlier dataset. Make changes here. and call update with the second dataadapter.
3. Close connection with database 2
Look at your code above - I see both the connection.open calls at the end of the code just before the update call. How is the dataset going to fill up with data if the connection has not been initially opened?
Now, for the case where you would need to merge the data from the tables in these two datasets - you could use the merge() method with the dataset class. you could go ahead with that.
jmcilhinney
July 27th, 2005, 07:35 AM
Look at your code above - I see both the connection.open calls at the end of the code just before the update call. How is the dataset going to fill up with data if the connection has not been initially opened?You'll note that I commented on the fact that these calls were unnecessary in a previous post. They make no difference to whether the data gets retrieved though, as the call to Fill is made several lines earlier, which implicitly opens and closes the associated connection. There is no call to Close the connection after the Update but this should not be an issue because the Update is not within a transaction so changes should be committed immediately.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.