I have a disconnected datatable that contains a few records. I am using the following function to get the dataTable.
c# Code:
static System.Data.DataTable ReadSetUpTable(string queryStr,SqlConnection sc) { try { var command = new SqlCommand() {Connection = sc, CommandText = queryStr}; var dataAdapter = new SqlDataAdapter() {SelectCommand = command}; var dataTable = new System.Data.DataTable(); dataAdapter.Fill(dataTable); return dataTable; } catch (Exception) { throw; } }
No issues so far. What I want to know is if there's an easy to populate this dataTable into another schema using a different connection string. For the sake of this post, assume that there is a table with two columns
Create Table Student(StudentId int, StudentName varchar2(50));
I wish to fill this table with the dataTable that I have in the above code.
I could do it using a command Object and an insert statement. For example this code:
c# Code:
static int LoadDataTable(OracleConnection oc, System.Data.DataTable dataTable) { try { var command = new OracleCommand { CommandText = "INSERT INTO STUDENT (STUDENTID, STUDENTNAME) VALUES(:studentid, :studentname)", CommandType = CommandType.TableDirect, Connection = oc }; var op1 = new OracleParameter { ParameterName = "StudentId", Size = 6, OracleDbType = OracleDbType.Int, Direction = System.Data.ParameterDirection.Input }; command.Parameters.Add(op1); var op2 = new OracleParameter { ParameterName = "studentName", OracleDbType = OracleDbType.Varchar2, Size = 50, Direction = System.Data.ParameterDirection.Input }; command.Parameters.Add(op2); foreach (var row in dataTable.Rows) { command.Parameters("studentId").Value = int.Parse(row[0].ToString()); command.Parameters("studentName").Value = row[1].ToString(); command.ExecuteNonQuery(); } } catch(Exception) { throw; } }
Is there a quicker and easier way where I will have to loop through the records?




Reply With Quote