Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 public void getdata()
     {
      try
      {
      using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM aa", con))
       {
      cmd.CommandType = CommandType.Text;
      using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
        {
     using (DataTable dt = new DataTable())
     {
     sda.Fill(dt);

   for (int i = 0; i < dt.Rows.Count; i++)
   {
   id =(string) dt.Rows[i]["id"];
    }

        // String query = "insert into nok_data_log(ID,Date_,Primary_Barcode,Station1_Status,Station2_Status,Station3_Status,Station4_Status,Station5_Status,Station6_Status,Station7_Status,Station8_Status,Station9_Status,Station1_Bcd1,Station1_Bcd2,Station1_Bcd3,Station2_Bcd1,Station2_Bcd2,Station2_Bcd3,Station3_Bcd1,Station3_Bcd2,Station3_Bcd3,Station4_Bcd1,Station4_Bcd2,Station4_Bcd3,Station5_Bcd1,Station5_Bcd2,Station5_Bcd3,Station6_Bcd1,Station6_Bcd2,Station6_Bcd3,Station7_Bcd1,Station7_Bcd2,Station7_Bcd3,Station8_Bcd1,Station8_Bcd2,Station8_Bcd3,Station9_Bcd1,Station9_Bcd2,Station9_Bcd3) values(@ID, @Date_, @Primary_Barcode, @Station1_Status,@Station2_Status,@Station3_Status,@Station4_Status,@Station5_Status,@Station6_Status,@Station7_Status,@Station8_Status, @Station9_Status, @Station1_Bcd1, @Station1_Bcd2, @Station1_Bcd3, @Station2_Bcd1, @Station2_Bcd2, @Station2_Bcd3, @Station3_Bcd1, @Station3_Bcd2, @Station3_Bcd3, @Station4_Bcd1, @Station4_Bcd2, @Station4_Bcd3, @Station5_Bcd1, @Station5_Bcd2, @Station5_Bcd3, @Station6_Bcd1, @Station6_Bcd2, @Station6_Bcd3,@Station7_Bcd1,@Station7_Bcd2,@Station7_Bcd3,@Station8_Bcd1,@Station8_Bcd2, @Station8_Bcd3,@Station9_Bcd1,@Station9_Bcd1,@Station9_Bcd1)";
 string query = "insert into bb(id,name)values (@id, @name)";

                        }
                    }
                }
}
            catch (Exception ex)
            {
                LogFileWrite("getdata,frmAuto : " + ex.Message);
            }
        }


What I have tried:

INSERT and DELETE in single operation (moving from one table to other in c# using mysql)

eg.
i have a table table1, table record ,save in another table table2
& delete record from table1 create c# function winform
actually can't understood how to solve plz help
Thanks in advance! 
Posted
Updated 13-Mar-21 1:58am
v2

1 solution

The way you're trying to do it is completely wrong!

You should avoid of copying data row-by-row (record-by-record), due to tons of reasons.

If the structure of tables is the same, you can use INSERT INTO[^]:
SQL
INSERT INTO Table4 (Field1, Field2, ... FieldN)
SELECT Field1, Field2, ... FieldN
FROM (
  SELECT Field1, Field2, ... FieldN
  FROM Table1
  UNION ALL
  SELECT Field1, Field2, ... FieldN
  FROM Table2
  UNION ALL
  SELECT Field1, Field2, ... FieldN
  FROM Table3
) A


In case you need to get data from 3 tables and they are related, you need to use JOIN:

SQL
INSERT INTO Table4 (Field1, Field2, ... FieldN)
SELECT Field1, Field2, ... FieldN
FROM (
  SELECT t1.Field1, t2.Field2, ... t3.FieldN
  FROM Table1 t1 
    INNER|OUTER|LEFT|RIGHT JOIN Table2 t2 ON t1.PrimaryKey = t2.ForeignKey
    INNER|OUTER|LEFT|RIGHT JOIN Table3 t3 ON t2.PrimaryKey = t3.ForeignKey
) A


For further details, please see: Visual Representation of SQL Joins[^]
 
Share this answer
 

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