Useful when exporting the records from one Database(Oracle) to another Database(Oracle) table .
using Oracle.DataAccess.Client;
public static void Create_ORA_Entry(string connStr, DataTable dataTable, string tableName)
{
DataTable dt_dest = Db.ExecuteDataTable_ORACLE(connStr, "select * from tableName where [column_ID]=" + dataTable.Rows[0]["COLUMN_ID"].ToString(), "tableName");
if (dt_dest.Rows.Count > 0)
{
return; //record already exists! no action taken.
}
string columns = string.Join(","
, dataTable.Columns.Cast<DataColumn>().Where(c => dt_dest.Columns.Contains(c.ColumnName)).Select(c => c.ColumnName));
string values = string.Join(","
, dataTable.Columns.Cast<DataColumn>()
.Where(c => dt_dest.Columns.Contains(c.ColumnName)).Select(c => string.Format(":{0}", c.ColumnName)));
String sqlCommandInsert = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableName, columns, values);
using (var con = new OracleConnection(connStr))
using (var cmd = new OracleCommand(sqlCommandInsert, con))
{
con.Open();
foreach (DataRow row in dataTable.Rows)
{
cmd.Parameters.Clear();
foreach (DataColumn col in dataTable.Columns.Cast<DataColumn>().Where(c=>dt_dest.Columns.Contains(c.ColumnName)))
{
cmd.Parameters.Add(col.ColumnName, row[col]);
}
int inserted = cmd.ExecuteNonQuery();
}
}
}
public static DataTable ExecuteDataTable_ORACLE(string dataConnectionString,
string sql, string tableName)
{
OracleDataAdapter oraAD = new OracleDataAdapter(sql, dataConnectionString);
DataTable dt = new DataTable(tableName);
try
{
oraAD.Fill(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oraAD.Dispose();
}
return dt;
}