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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:34:29+00:00 2026-06-10T22:34:29+00:00

I have two similar tables on different databases. Database1/TableA Database2/TableA I want to fetch

  • 0

I have two similar tables on different databases.

Database1/TableA

Database2/TableA

I want to fetch a row from one table and insert it into other table on other server. Like:

Database1/TableA
Id        State            Name
500        OH              John  [Fetch this row]

Database2/TableA
Id        State            Name
1          OH              John  [Insert and fetch PK '1']

I tried this using bulkcopy and it works fine.

But problem is I need to get PK from the new insert as I need to populate another child table.

Is there any better way to achieve this? Please on C# code, no database linking or SQL queries. Just C# solutions. Or if query can be used in C# code that is fine. Any working example code with Dataset or Datarow will be great help.

Thanks!

  • 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-10T22:34:31+00:00Added an answer on June 10, 2026 at 10:34 pm

    First you need to get the row(s) from Database.TableA. You could for example use a SqlDataAdapter with a DataTable or a SqlDataReader.

    SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

    You can use SqlCommand.ExecuteScalar to execute the insert command and retrieve the new ID in one query.

    const String sqlSelect = "SELECT COL1,COl2,Col3 FROM TableA WHERE COL1=@COL1;"
    const String sqlInsert = "INSERT INTO TableA (COl2,Col3)VALUES (@Col2,@Col3);"
                           + "SELECT CAST(scope_identity() AS int)";
    using (var con1 = new SqlConnection(db1ConnectionString))
    using (var con2 = new SqlConnection(db2ConnectionString))
    {
        con1.Open();
        con2.Open();
        using(var selectCommand = new SqlCommand(sqlSelect, con1))
        {
            selectCommand.Parameters.AddWithValue("@COL1", 4711);
            using (var reader = selectCommand.ExecuteReader())
            {
                if (reader.Read())
                {
                    int newID;
                    using (var insertCommand = new SqlCommand(sqlInsert, con2))
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            insertCommand.Parameters.AddWithValue("@" + reader.GetName(i), reader[i]);
                        }
                        newID = (int)insertCommand.ExecuteScalar();
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two similar tables in different databases, I want to insert into one
I have two tables with similar columns - let's say table A with column
I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables one with cities and one with pois. I want find
I have two tables that get joined regularly. Table One is about 1 Million
I have two similar queries on same table and same where condition but different
I have two database tables containing similar data - one is the amount of
I have two similar tables table_a and table_b table_a is my current data and
I have two tables that have virtually identical content and very similar structure. They
I have two similar codes. The first one works as expected. urlpatterns = patterns('',

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.