Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>this is what i do in data migration scenarios from one SQL Server to another or DataFiles to SQL:</p> <ol> <li>Create the new Table on the destination SQL Server (Column names, Primary Key etc.)</li> <li>Load existing Data into a DataTable (Thats what you did already)</li> <li>Now Query the new Table with the DataAdapter into another DataTable (Same as you did with the excel file except you now query the SQL Table.)</li> <li>Load OldData from 'table' into 'newTable' using DataTable Method "Load()"</li> </ol> <pre class="lang-cs prettyprint-override"><code>string PathConn = (MYSQL Connection String goes here) OleDbConnection conn = new OleDbConnection(PathConn); conn.Open(); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + loadTextBox.Text + "$]", conn); newTable = new DataTable(); myDataAdapter.Fill(newTable); Now use the Load() Method on the new table: newTable.Load(table.CreateDataReader(), &lt;Specify LoadOption here&gt;) </code></pre> <p>Matching columns will be imported into the new DataTable. (You can ensure the mapping through using Aliases in the select statements)</p> <p>After Loading the existing Data into the new Table you will be able to use an DataAdapter to write the changes back to database. </p> <p>Example for writing data back: ConnString - connection String for DB, SelectStmt (can use the same as you did on the empty Table before) and provide the newTable as dtToWrite</p> <pre><code>public static void writeDataTableToServer(string ConnString, string selectStmt, DataTable dtToWrite) { using (OdbcConnection odbcConn = new OdbcConnection(ConnString)) { odbcConn.Open(); using (OdbcTransaction trans = odbcConn.BeginTransaction()) { using (OdbcDataAdapter daTmp = new OdbcDataAdapter(selectStmt, ConnString)) { using (OdbcCommandBuilder cb = new OdbcCommandBuilder(daTmp)) { try { cb.ConflictOption = ConflictOption.OverwriteChanges; daTmp.UpdateBatchSize = 5000; daTmp.SelectCommand.Transaction = trans; daTmp.SelectCommand.CommandTimeout = 120; daTmp.InsertCommand = cb.GetInsertCommand(); daTmp.InsertCommand.Transaction = trans; daTmp.InsertCommand.CommandTimeout = 120; daTmp.UpdateCommand = cb.GetUpdateCommand(); daTmp.UpdateCommand.Transaction = trans; daTmp.UpdateCommand.CommandTimeout = 120; daTmp.DeleteCommand = cb.GetDeleteCommand(); daTmp.DeleteCommand.Transaction = trans; daTmp.DeleteCommand.CommandTimeout = 120; daTmp.Update(dtToWrite); trans.Commit(); } catch (OdbcException ex) { trans.Rollback(); throw ex; } } } } odbcConn.Close(); } } </code></pre> <p>Hope this helps.</p> <p>Primary Key on the newTable is necessary, otherwise you might get a CommandBuilder exception.</p> <p>BR</p> <p>Therak</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload