473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Trying to update a database with the contents of a dataset

11
Hi

I have a dataset which contains the entire contents of a database. During the course of my program some records are added. I am having difficulty trying to find an SQL command that will transfer the added rows in one go (without using a tableadapter which I'm told I can't define programmatically).

What is the quickest way to add all the added records at once? (can I even do this?) do I have to define an enumerator and add each row individually? Note that dataset contains exactly the same columns as the database. What's wrong with my SQL command? Do I need to change the SQL command to explicitly reference the datasetcolumns eg updtable.columns("PriceEach")??

Expand|Select|Wrap|Line Numbers
  1.         Dim updTable As DataTable = GblDataSet.Tables("Inventory")
  2.  
  3.         myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory ('PriceEach', 'RetailPrice', 'Markup', 'Shipping(each)', 'Notes', 'POsUsed', 'PartID', 'None', 'NumOnOrder') VALUES ('PriceEach', 'RetailPrice', 'Markup', 'Shipping(each)', 'Notes', 'POsUsed', 'PartID', 'None', 'NumOnOrder')")
  4.  
  5.  
  6.         myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.Added))
Any help would be most appreciated.
Oct 23 '07 #1
4 1530
AlexW
11
Do I Have to make a really long SQL string to update multiple records at once eg?

INSERT INTO tablename (blah) VALUES ('asdf'); INSERT INTO tablename (blah) VALUES ('asdf'); INSERT INTO tablename (blah) VALUES ('asdf'); INSERT INTO tablename (blah) VALUES ('asdf');

Can somebody tell me if there is a more elegant solution?
Oct 23 '07 #2
Jim Doherty
897 Expert 512MB
Do I Have to make a really long SQL string to update multiple records at once eg?

INSERT INTO tablename (blah) VALUES ('asdf'); INSERT INTO tablename (blah) VALUES ('asdf'); INSERT INTO tablename (blah) VALUES ('asdf'); INSERT INTO tablename (blah) VALUES ('asdf');

Can somebody tell me if there is a more elegant solution?
You can INSERT directly from a SELECT ie:

INSERT INTO tblBlah2 (field1,Field2,Field3)
SELECT (field1,field2,field3 FROM tblBlah1 WHERE Field3='Blahblahblah'

Jim :)
Oct 24 '07 #3
AlexW
11
Ok, but I still can't get the database to update. The code for my commit changes to database function is shown below. The problem arises when I start trying to update the database with the inserted rows (towards the end). You can see I've tried lots of different approaches (commented out) but am still getting nowhere.... still get message saying "syntax error in INSERT INTO" statement when I try and update the database. Can anyone help???

Expand|Select|Wrap|Line Numbers
  1. Public Function commitChangesToDatabase(ByVal tableName As String)
  2.         Dim commandString As String
  3.         Dim ConnectionString As String
  4.         Dim myDataAdapter As OleDbDataAdapter
  5.         Dim test As Object
  6.         ' initialisation
  7.         'connection string should point to the location of the inventory
  8.         ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:\Documents and Settings\User\Desktop\Devel\inventory analysis" + "\Inventorytest.mdb"
  9.         commandString = "Select * From " + tableName
  10.  
  11.         'setting up the dataAdapter
  12.         myDataAdapter = New OleDbDataAdapter(commandString, ConnectionString)
  13.         Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(myDataAdapter)
  14.  
  15.         'filling the dataset using the dataAdapter
  16.         Dim updTable As DataTable = GblDataSet.Tables("Inventory")
  17.  
  18.         ' First process deletes.
  19.         myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.Deleted))
  20.  
  21.         ' Next process updates.
  22.         myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.ModifiedCurrent))
  23.  
  24.         ' Finally, process inserts. Note that everything seems to work up to this point and That I have tried a few approaches here but most are commented out
  25.         'myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory (Select *)")
  26.         'myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory ('PriceEach', 'RetailPrice') select ('PriceEach', 'RetailPrice') from Inventory")
  27.         'myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory (PriceEach, RetailPrice) select (PriceEach, RetailPrice")
  28.         'myDataAdapter.UpdateCommand.CommandText = "INSERT VALUES"
  29.  
  30.       'trying different methods of updating database
  31.       'myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.Added), "Inventory")
  32.         'myDataAdapter.Update(updTable.DataSet, "Inventory")
  33.         myDataAdapter.Update(GblDataSet, tableName)
  34.  
  35.     End Function
Note that I'm trying to copy the added rows in the inventory table of my dataset to the inventory table of the database
Oct 24 '07 #4
Jim Doherty
897 Expert 512MB
Ok, but I still can't get the database to update. The code for my commit changes to database function is shown below. The problem arises when I start trying to update the database with the inserted rows (towards the end). You can see I've tried lots of different approaches (commented out) but am still getting nowhere.... still get message saying "syntax error in INSERT INTO" statement when I try and update the database. Can anyone help???

Expand|Select|Wrap|Line Numbers
  1. Public Function commitChangesToDatabase(ByVal tableName As String)
  2. Dim commandString As String
  3. Dim ConnectionString As String
  4. Dim myDataAdapter As OleDbDataAdapter
  5. Dim test As Object
  6. ' initialisation
  7. 'connection string should point to the location of the inventory
  8. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:\Documents and Settings\User\Desktop\Devel\inventory analysis" + "\Inventorytest.mdb"
  9. commandString = "Select * From " + tableName
  10.  
  11. 'setting up the dataAdapter
  12. myDataAdapter = New OleDbDataAdapter(commandString, ConnectionString)
  13. Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(myDataAdapter)
  14.  
  15. 'filling the dataset using the dataAdapter
  16. Dim updTable As DataTable = GblDataSet.Tables("Inventory")
  17.  
  18. ' First process deletes.
  19. myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.Deleted))
  20.  
  21. ' Next process updates.
  22. myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.ModifiedCurrent))
  23.  
  24. ' Finally, process inserts. Note that everything seems to work up to this point and That I have tried a few approaches here but most are commented out
  25. 'myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory (Select *)")
  26. 'myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory ('PriceEach', 'RetailPrice') select ('PriceEach', 'RetailPrice') from Inventory")
  27. 'myDataAdapter.UpdateCommand = New OleDbCommand("INSERT INTO Inventory (PriceEach, RetailPrice) select (PriceEach, RetailPrice")
  28. 'myDataAdapter.UpdateCommand.CommandText = "INSERT VALUES"
  29.  
  30. 'trying different methods of updating database
  31. 'myDataAdapter.Update(updTable.Select(Nothing, Nothing, DataViewRowState.Added), "Inventory")
  32. 'myDataAdapter.Update(updTable.DataSet, "Inventory")
  33. myDataAdapter.Update(GblDataSet, tableName)
  34.  
  35. End Function
Note that I'm trying to copy the added rows in the inventory table of my dataset to the inventory table of the database

OK lets work through some of your failures then you will see where it is you have gone wrong

Line 25 insertion syntax is incorrect

Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Inventory (Select *)"
You are not telling it where to insert from in other words (select *) but from what which table FROM? In addition remove those brackets surrounding it the string merely needs to be commenced with a speechmarks and end with a speechmark drop the bracketing around it

Line 26 insertion syntax is incorrect

I know this is not what you had in mind but If you had wanted an insert of the literal words 'PriceEach' and 'RetailPrice' then this is what you would use and here you would not need to reference a table FROM because the words are literal and not being fetched from a table in that case if you understand me.

Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Inventory ( PriceEach, RetailPrice )
  2. SELECT 'PriceEach' AS PriceEach, 'RetailPrice' AS RetailPrice;"
Your mention wanting to insert field data back into itself from the same table referenced thats ok.

This is what you need plain and simple nothing extra no extra bracketing

Expand|Select|Wrap|Line Numbers
  1.  
  2. "INSERT INTO Inventory (PriceEach, RetailPrice)
  3. SELECT Inventory.PriceEach, Inventory.PriceEach
  4. FROM Inventory;"
  5.  
Again other than the brackets laid out my sample above you don't need any more than that to be synxtax correct for an insert derived from a select in Access

As an aside if you are unsure of the textual requirements for SQL its a simple thing to actually open access and let access do it for you by simply playing with the query grid and that inventory table you might particularly want to look at how 'Access' creates an 'append' query to cater for your requirement to insert data from inventory back into itself. You will be able to see the SQL it generates by looking in the SQL window (query design....menubar...SQL view) You could paste that into your application code and surround it with speechmarks to make it a string


Hope this helps you

Jim :)
Oct 24 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: PAUL EDWARDS | last post by:
I have a windows form that is bound to a datatable. In VB6 I could just update the field contents and it would be updated in the database, however if I update the text property of the control from...
0
by: Tino Mclaren | last post by:
Hi, Got a small app based around a access database. In ADO.net i simply called update method to uptade my database with dataset contents. i.e. OleDbDataAdaptor.Update (Dataset.tableName) Why...
2
by: Niyazi | last post by:
Hi, I have not understand the problem. Before all the coding with few application everything worked perfectly. Now I am developing Cheque Writing application and when the cheque is clear the...
6
by: Babu Mannaravalappil | last post by:
Can somebody please help me figure out why the following method exceptions out? Execution at the line marked with ********** hangs for about 15 seconds and then I get an error that says an...
2
by: Chris Fink | last post by:
Any idea why the database table is not being updated with the contents of the excel file loaded into the dataset? Any help is appreciated! // load database table, shell only string cnString =...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
3
by: Steve Teeples | last post by:
I am new to database work. I have a very simple database with only two tables linked by a 1:1 relationship. I've read the tables contents into a dataset and bound the two tables to textbox...
3
by: Sonu | last post by:
Hello, I have a winform1 that contains the Datagrid and in winform2 you can add new values to the database for datagrid. I cannot update the datagrid on winform2 close event! All I want is to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.