C# – Save a DataTable into a database table

.netc++datatablemysql

I am generating a Data Table from my application and I would like to save the whole Data Table into one database table.

DataTable ds = //add the info from the queue in the application

The DataTable is getting generated but What to do next.Show me some syntax.I dont really need the select statement there either, i just want to insert all the info from the DataTable into already created db table(update the table).
I will use the ODBC connection to access the MYSQL database

i want to update the data into the database through dataset directly

public void update(DataTable ds)
{
  try
  {  
    lock (myLockHolder)
    {
      X1 = 1;
      OdbcConnection con = 
      new OdbcConnection(LocalConnection.GetLocalConnetionString());
      OdbcCommand cmd;
      OdbcDataAdapter da;
      DataSet ds1=new DataSet();
      string query = "";
      query = "update parameter" + Environment.NewLine;
      query += "set paramvalue=paramvalue,date_logged1=date_logged1," 
        + Environment.NewLine;
      query += "  Quality=Quality,date_logged=date_logged" 
        + Environment.NewLine;
      query += " where itemID=itemID";
      cmd = new OdbcCommand(query, con);
      da = new OdbcDataAdapter(cmd);
      ds1=new DataSet();
      ds1.Tables.Add(ds);
      da.Update(ds1);                   

    }
  }
  catch { }
  finally { }
}

It will be used like this method catch this exception
"Update unable to find TableMapping['Table'] or DataTable 'Table'."

Best Solution

You should create insert sql string, loop thorugh all rows in your datatable, add parameters for each column in datatable, and execute this query.

string sql = "INSERT INTO T (A, B, C) VALUES (@A, @B, @C)"; 
using (SqlConnection conn = new SqlConnection(connectionString))
{   
   conn.Open();   
   foreach (DataRow r in myTable.Rows)   
   {      
       SqlCommand cmd = conn.CreateCommand();      
       cmd.CommandText = sql;      
       cmd.Parameters.AddWithValue("@A", r["A"]);      
       cmd.Parameters.AddWithValue("@B", r["B"]);      
       cmd.Parameters.AddWithValue("@C", r["C"]);      
       cmd.ExecuteNonQuery();   
   }
}