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 7430829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:12:08+00:00 2026-05-29T09:12:08+00:00

I have a DataTable which is generated from .xls table. I would like to

  • 0

I have a DataTable which is generated from .xls table.

I would like to store this DataTable into an existing table in SQL Server database.

I use SqlBulkCopy to store rows which have unique PK.

Problem is, I also have other rows which have same PK as SQL Server table but contain cells with different value compared to SQL Server table.

In short:

Let’s say in my DataTable I have a row like this:

id(PK) | name | number

005 | abc | 123

006 | lge | 122

For my SQL server I have sth like this;

id(PK) | name | number

004 | cbs | 345

005 | lks | 122

Now you see the row 006 can be uploaded straight away into SQL Server using SqlBulkCopy. On the other hand the row 005 can’t be inserted using it since SQL server table contains row with identical PK.

Now I tried to manually extract the row. Extract each single cell into an ArrayList then generate an UPDATE Table statement afterwards. However this method seems to be unfeasible as I have so many rows to process.

I am looking for a better method to achieve this goal.

Any help is appreciated.

Thank’s

  • 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-29T09:12:09+00:00Added an answer on May 29, 2026 at 9:12 am

    Use the below code:

    C# Side code for reading data from DataTable and preparing the XML data:

    DataTable dt = new DataTable();
    StringBuilder sb = new StringBuilder();
    
    sb.Append("<R>");
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        sb.Append("<C><ID>" + dt.Rows[0].ToString() + "</ID>");
        sb.Append("<N>" + dt.Rows[1].ToString() + "</N>");
        sb.Append("<I>" + dt.Rows[2].ToString() + "</I></C>");
    }
    
    sb.Append("</R>");
    
    ///pass XML string to DB side
    ///
    //sb.ToString(); //here u get all data from data table as xml format
    

    Database side Stored Procedure (you will need to update your table name):

    CREATE PROCEDURE dbo.UpdateData 
        -- Add the parameters for the stored procedure here
        @data       XML
    AS
    BEGIN
        SET NOCOUNT ON;
    
        -- keep data into temp table
        create table #tmp_data (id nchar(2),name varchar(20), number int)
    
        DECLARE @XMLDocPointer INT  
        EXEC sp_xml_preparedocument @XMLDocPointer OUTPUT, @DATA
    
        INSERT INTO #tmp_data(id,name,number)
        SELECT  ID,N,I
        FROM OPENXML(@XMLDocPointer,'/R/C',2)
        WITH(
                ID  nchar(30),
                N   VARCHAR(20),
                I   int
            )
    
        EXEC sp_xml_removedocument @XMLDocPointer
    
        begin tran
            -------------------INSERT not existing ones
            INSERT INTO TABLE (id,name,number)
            SELECT id,name,number
            FROM #tmp_data
            WHERE NOT EXISTS
            (
                SELECT 1
                FROM TABLE
                WHERE ID = #tmp_data.ID
            )
    
            --- update existing ones
            UPDATE  TABLE
            SET name = #tmp_data.name, number = #tmp_data.number
            FROM #tmp_data
            WHERE #tmp_data.id = TABLE.id
    
            commit tran
    
        if(@@error <> 0)
            rollback tran
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table, generated from a LINQ query on a datatable, which has
I have a datatable which has been dynamically generated from FoxPro tables using a
I have a DataTable which I want to save to a SQLite Database Table.
I have a DataTable which I select from database (Well, these data cross several
In C#/ASP.NET 3.5, I have a DataTable which pulls rows from the database. I
I have a DataSet which I get a DataTable from that I am being
I have a datatable which contains all these records (see SQL query). Instead of
I have a DataTable which I get by an upload of CSV document from
I have a DataTable which contains data in this format. I want to convert
I have to create xml after reading from a datatable, in C# application. This

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.