Click here to Skip to main content
16,022,054 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I copy data from one table to another without transaction, the program works fine and with the transaction displays a message.
Kindly help to achieve this,
Regards
TIA

What I have tried:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      oleCon.Open()
      Dim strCategory As String = " SELECT Category.ID, Category.Label FROM Category WHERE Category.ID = 1 "
      Dim olecmd As New OleDbCommand(strCategory, oleCon)
      Dim trans As OleDbTransaction = oleCon.BeginTransaction
      Dim dreaderCategory As OleDbDataReader = olecmd.ExecuteReader
      Dim intID As Integer
      Dim strLabel As String = String.Empty
      While dreaderCategory.Read
          intID = dreaderCategory("ID")
          strLabel = dreaderCategory("Label")
      End While
      dreaderCategory.Close()
      Dim strCategory_2 As String = "INSERT INTO Category_2 (ID,Label) VALUES (@ID,@Label)"
      Dim olecomCategory_2 As New OleDbCommand(strCategory_2, oleCon)
      olecomCategory_2.Parameters.AddWithValue("@ID", intID)
      olecomCategory_2.Parameters.AddWithValue("@Label", strLabel)
      olecmd.Transaction = trans
      olecomCategory_2.ExecuteNonQuery()
      oleCon.Close()

      Dim strProduct As String = "SELECT Product.ID, Product.Label, Product.ID_Cat FROM Category, Product WHERE Product.ID_Cat = 1 "
      Dim intIDProduct As Integer
      Dim strLabelProduct As String
      Dim intID_CatProduct As Integer
      Dim olecmdProduct As New OleDbCommand(strProduct, oleCon)
      oleCon.Open()
      Dim dreaderProduct As OleDbDataReader = olecmdProduct.ExecuteReader
      While dreaderProduct.Read
          intIDProduct = dreaderProduct("ID")
          strLabelProduct = dreaderProduct("Label")
          intID_CatProduct = dreaderProduct("ID_Cat")
          Dim strProduct_2 As String = "INSERT INTO Product_2 (ID,Label,ID_Cat) VALUES (@ID,@Label,@ID_Cat)"
          Dim olecomProduct_2 As New OleDbCommand(strProduct_2, oleCon)
          olecomProduct_2.Parameters.AddWithValue("@ID", intIDProduct)
          olecomProduct_2.Parameters.AddWithValue("@Label", strLabelProduct)
          olecomProduct_2.Parameters.AddWithValue("@ID_Cat", intID_CatProduct)
          'olecomDetailCmd.Transaction = tran
          olecmd.Transaction = trans
          olecomProduct_2.ExecuteNonQuery()
      End While
      trans.Commit()
  End Sub
Posted
Updated yesterday
v7

A better solution would be to do this in the database, assuming it's supported, rather than in code.

For example, with SQL Server, you can do this in a single command:
SQL
INSERT INTO Category_2 (ID, Label)
SELECT ID, Label 
FROM Category 
WHERE ID = @CategoryID;

INSERT INTO Product_2 (ID, Label, ID_Cat)
SELECT ID, Label, ID_Cat 
FROM Product 
WHERE ID_Cat = @CategoryID;
 
Share this answer
 
Comments
HichSkill yesterday    
I tried this method, but the problem is that they are not the same ID and it is not auto.
Richard Deeming yesterday    
I'm sure that means something to you, but I can't make any sense of what you've said.

The query I posted will do exactly the same thing that your code would, if your code worked:

* Select the details of a specific category;
* Insert the same details into Category_2;
* Select the details of all products in the category;
* Insert the same details into Product_2;

If you're starting from non-working code that wouldn't do what you want it to anyway, then you're missing a lot of information from your question!
HichSkill 23hrs ago    
For your example, is it possible to put the ID by another local variable? For example
INSERT INTO Product_2 ('1', Label, ID_Cat)
SELECT ID, Label, ID_Cat
FROM Product
WHERE ID_Cat = @CategoryID;
Richard Deeming 23hrs ago    
Yes - for example, if you want the second category to have a different ID:
INSERT INTO Category_2 (ID, Label)
SELECT @Category2ID, Label 
FROM Category 
WHERE ID = @CategoryID;

INSERT INTO Product_2 (ID, Label, ID_Cat)
SELECT ID, Label, @Category2ID 
FROM Product 
WHERE ID_Cat = @CategoryID;
Richard Deeming 23hrs ago    
If you want the second product(s) to have a different ID, it's slightly more complicated. You'd need to work out how you want to assign the new IDs.

For example (assuming SQL Server):
DECLARE @MaxProductID int;
SELECT @MaxProductID = MAX(ID) FROM Product_2;
If @MaxProductID Is Null SET @MaxProductID = 0;

INSERT INTO Product_2 (ID, Label, ID_Cat)
SELECT @MaxProductID + ROW_NUMBER() OVER (ORDER BY ID), Label, @Category2ID 
FROM Product 
WHERE ID_Cat = @CategoryID;

ROW_NUMBER (Transact-SQL) - SQL Server | Microsoft Learn[^]
The is pretty explicit. You cannot have an open DataReader and execute other queries ON THE SAME CONNECTION at the same time. You either have to use the DataReader to read all of the data into an appropriate data structure, close the DataReader, then process each record you have cached,

OR

You use TWO connection objects. One for the DataReader and one for the queries you're executing against each of the records your getting from the reader.
 
Share this answer
 
Comments
HichSkill yesterday    
Thank you for your answer.
My problem is that I want to copy the details and information from one table to another. For example, transfer a quote to a purchase order in the sales management, do you have an example to modify my code? I am an apprentice in programming. Thank you in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900