简体   繁体   中英

How to Insert DataTable Data or Select Query Data into Different Oracle Database Table in C# Console App

I am totally new in Dot Net and in Programming Field.

I have to Send Some Data from One Table in Email in one console app. It is working fine. I am doing this by retrieving Data into DataTable.

Now i have to insert same Data into Different Database Table.

So how i can do this. Please share a simple way.

(In DataTable data is coming by Single Select Query, So is there any way to solve this problem using this Select command with Insert command, Like Insert into Table Values (Seelct Command))

There are probably no easy ways to do it from the original DataTable (unless you decide to parse the original Select query and replace table name), so the simplest way is to just:

  1. Copy the DataTable - https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.copy?view=netframework-4.8 . Perhaps you will have to set row states to Added - C# DataSet, DataAdapter -- How to change row state to invoke an INSERT? .
  2. Create a new DataAdapter with the same query you used to retrieve the original DataTable but with the target table.
  3. Invoke the DataUpdater.Update on the new DataTable

It will look something like(didn't actually test it, though)

private const String SelectDataTableTemplate = @"SELECT ... FROM {0}";
...
var originalDataTable = GetDataTable();
var copyOfDataTable = originalDataTable.Copy();
foreach(var row in copyOfDataTable.Rows)
{
    row.AcceptChanges(); // sets DataRowState.Unchanged
    row.SetAdded(); 
}
var dataAdapter = new OracleDataAdapter(
    selectCommandText: String.Format(SelectDataTableTemplate, "AnotherTableName"), 
    selectConnection: _connection));
dataAdapter.Update(copyOfDataTable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM