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

  • SEARCH
  • Home
  • 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 8051027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:13:00+00:00 2026-06-05T07:13:00+00:00

I need to write a method in C# to save all rows in a

  • 0

I need to write a method in C# to save all rows in a given datatable back to the database, but I cannot depend on the rowstate of each row. I can’t think of how best to do this without extensive looping through the database table data or constant database querying.

I want to essentially perform the following insert / update for each row in the datatable, but more efficiently:

INSERT INTO table
(   col1,
    col2    )
SELECT
    'value1',
    'value2'
FROM dual
WHERE NOT EXISTS ( SELECT * FROM table WHERE col1 = 'value1' );

UPDATE table
SET col2 = 'value2'
WHERE col1 = 'value1';

I was thinking a structure like this would be the most efficient:

private void SaveDataTable(DataTable dataTable)
{
    //Remove data from the dataTable where it already exists in the database

    //Determine if the values are to be inserted or updated

    //Save relevant data
}

Any ideas on how best to accomplish this?

  • 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-06-05T07:13:00+00:00Added an answer on June 5, 2026 at 7:13 am

    While I am unable to avoid querying for each row in the given datatable, I think this will work for now. Thanks for your responses; they were all very helpful.

    (Oracle specific):

    internal override bool RecordExists(DataRow row)
    {
        string query = string.Concat("SELECT 1 FROM ", row.Table.TableName, " ", GetDbTableFilter(row, row.Table.PrimaryKey), " AND ROWNUM = 1");
    
        using(OracleCommand cmd = new OracleCommand(query, (OracleConnection)_connectionLibrary.CurrentConnection))
            using(OracleDataReader reader = cmd.ExecuteReader())
                return (reader != null && reader.Read());
    }
    
    internal override string GetDbTableFilter(DataRow row, DataColumn[] columns)
    {
        string filter = "";
    
        foreach (DataColumn column in columns)
        {
            if (!string.IsNullOrEmpty(filter))
                filter += " AND ";
    
            if (string.IsNullOrEmpty(row[column].ToString()))
            {
                filter += string.Concat("(", column.ColumnName, " IS NULL OR TRIM(", column.ColumnName, ") = '')");
                continue;
            }
    
            if (column.DataType.Equals(typeof(DateTime)))
            {
                filter += string.Concat(column.ColumnName, " = TO_DATE('", row[column].ToString(), "')");
                continue;
            }
    
            filter += string.Concat(column.ColumnName, " = '", row[column].ToString(), "'");
        }
    
        return string.Concat("WHERE ", filter);
    }
    
    public override void SaveDataTable(DataTable dataTable)
    {
        dataTable.AcceptChanges();
    
        foreach (DataRow row in dataTable.Rows)
            if(RecordExists(row))
                row.SetModified();
            else
                row.SetAdded();
    
        using (OracleDataAdapter adapter = new OracleDataAdapter(string.Format("SELECT * FROM {0}", dataTable.TableName), (OracleConnection)_connectionLibrary.CurrentConnection))
            using (OracleCommandBuilder commandBuilder = new OracleCommandBuilder(adapter))
            {
                commandBuilder.SetAllValues = true;
                commandBuilder.ConflictOption = ConflictOption.OverwriteChanges;
    
                adapter.InsertCommand = commandBuilder.GetInsertCommand(true);
                adapter.UpdateCommand = commandBuilder.GetUpdateCommand(true);
    
                adapter.Update(dataTable);
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write effective and quick method to search byte array for given
I'm writing a 'Sudoku' program, and I need to write a method to check
I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a small console app (patch) that turns off the print
I need to write a shell script to be scheduled to run daily to
I need to write an app that reads a config file with info on
I need to write a large amount of data in my application. Data arrive
I need to write a python script that makes multiple HTTP requests to the
I need to write some code to trigger internet connectivity on a computer. By
I need to write a little ruby function that does word wrapping. I have

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.