Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4553324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:55:47+00:00 2026-05-21T16:55:47+00:00

I have a disconnected dataTable that contains a few records. I am using the

  • 0

I have a disconnected dataTable that contains a few records.

I am using the following function to get the dataTable.

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 NUMBER(6), 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:

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.Int32,
            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)
        {
            op1.Value = int.Parse(row[0].ToString());
            op2.Value = row[1].ToString();
            command.ExecuteNonQuery();
        }*/
            foreach (System.Data.DataRow row in dataTable.Rows)
            {
                row.SetAdded();
            }    

            var dataAdapter = new OracleDataAdapter() {InsertCommand = command};
            dataAdapter.Update(dataTable); //This updates the table, but all column values are NULL.

    }
    catch(Exception)
    {
        throw;
    }
} 

Is there a quicker and easier way where I will not have to loop through the records?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:55:47+00:00Added an answer on May 21, 2026 at 4:55 pm

    In your first block of code, you’re setting the SelectCommand. There’s also an InsertCommand, UpdateCommand, and DeleteCommand.

    These commands also exist on OracleDataAdapter objects… Since the DataTable is endpoint-neutral, all you would need to do is create an OracleCommand to do the insert, set it as the OracleDataAdapter's InsertCommand, and call oracleDataAdapter.Update(dataTable).

    Will modify this with more details as I check up on them.


    A good example of setting the InsertCommand is here. Note that when you add parameters to the command, the last value you pass to .Add( ... ) is the name of the column you’re mapping to.

    Because you’re retrieving the data into the DataTable, but not changing it, you’ll need to change the RowState of each row to ‘Added’ before calling oracleDataAdapter.Update(). You’ll need to do something like this:

    foreach (DataRow row in dataTable.Rows) {
        row.SetAdded();
    }
    

    Let me know if you need more code examples… from the code you’ve posted, I think you’ve pretty much got the gist of where I’m going here.


    Edit

    When you create the OracleParameters, you need to set the source column to the name of the column in the DataTable. By default, that’s the name returned by the select statment, so:

    var op1 = new OracleParameter {
                                    ParameterName = "StudentId",
                                    Size = 6,
                                    OracleDbType = OracleDbType.Int32,
                                    Direction = System.Data.ParameterDirection.Input
                                    SourceColumn = "StudentId" // If that's what it's called in the DataTable
                                  };
    command.Parameters.Add(op1);
    

    The AcceptChanges() method is on the DataTable, as is the HasErrors property (it also exists on the DataRows, and DataSets, too).

    AcceptChanges() only tells the DataTable that you’ve handled the updates to the database… the changes are committed to the database when you called oracleDataAdapter.Update(). The reason you call AcceptChanges() is to reset the row states… else the next time you updated the DataTable, you’d be trying to add rows that were already added.

    The property AcceptChangesDuringUpdate on the OracleDataAdapter will automatically call AcceptChanges() as part of the update to the database… I don’t usually do that, because I’m used to testing the HasErrors property and handling those before calling AcceptChanges()… however I’m reading that AcceptChanges() is called by default on the update now. I’m not familiar with the details of how rows that are in error are handled, if the changes are accepted, or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
I have two disconnected sql servers that have to have correlated queries run between
I have a Java web application that has a 'disconnected' Java Swing desktop app.
I have an ATL ActiveX control that raises three events (Connected, Authenticated, Disconnected) which
I have an android app that uses a background service for uploading data. When
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Say I have a DataGridView (named dataGridView ) that is displaying a Strongly Typed
I have a Database named CarsType.accdb there are four fields in the data base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.