0

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))

  • 2
    You can use "insert into tableX (fieldName1, fieldName2 ....) select firld1, field2 ... from tableY". BTW your question is not very clear. What do you mean by "send one table in email in one console app"? – Cetin Basoz Aug 02 '19 at 18:24
  • I have to send Email of Some Data in Table Format (Which is coming from Database). Also now i have to Insert Same Data in Different DataBase Table. – Shahnawaz Ahmad Aug 02 '19 at 18:57
  • Sorry but what do you mean by "I have to send Email of Some Data in Table Format (Which is coming from Database). Also now i have to Insert Same Data in Different DataBase Table"? – Cetin Basoz Aug 02 '19 at 19:00
  • Actually it is a Console app, which is sending Email to Manger about Plant ShutDown Details. So Sending Email with all Details is working fine. But now i have to add one more function in the program, which will do an insertion of the same Data in Different Oracle Database Table. – Shahnawaz Ahmad Aug 02 '19 at 19:03
  • Still it is not clear what you want and what you cannot do. – Cetin Basoz Aug 02 '19 at 19:04
  • Your question is not really clear in order to provide you with a specific answer however what I understand is that you are trying to send the data from the data table into the oracle database. You ncan take a look at https://stackoverflow.com/questions/39337413/inserting-data-into-oracle-database-using-c-sharp to see how to insert data with C#. Just iterate through the data table and send the row values to the insert function. – Walters Aug 02 '19 at 19:08
  • Which Data i am sending in email, i have to insert this data in another Database table. **Sorry for my Bad English**. Simply it is console app, which is basically retrieving Shutdown detail from Table. After retrieving it is sending same detail to Manager using Mail process. This part is working fine. But right now i have to do one more work. I have to insert the retrive data into another table. – Shahnawaz Ahmad Aug 02 '19 at 19:08

1 Answers1

0

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://learn.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);
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53