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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:15:02+00:00 2026-05-30T02:15:02+00:00

Using C# in Visual Studio, I’m inserting a row into a table like this:

  • 0

Using C# in Visual Studio, I’m inserting a row into a table like this:

INSERT INTO foo (column_name)
VALUES ('bar')

I want to do something like this, but I don’t know the correct syntax:

INSERT INTO foo (column_name)
VALUES ('bar')
RETURNING foo_id

This would return the foo_id column from the newly inserted row.

Furthermore, even if I find the correct syntax for this, I have another problem: I have SqlDataReader and SqlDataAdapter at my disposal. As far as I know, the former is for reading data, the second is for manipulating data. When inserting a row with a return statement, I am both manipulating and reading data, so I’m not sure what to use. Maybe there’s something entirely different I should use for 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-05-30T02:15:03+00:00Added an answer on May 30, 2026 at 2:15 am

    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.

    using (var con = new SqlConnection(ConnectionString)) {
        int newID;
        var cmd = "INSERT INTO foo (column_name)VALUES (@Value);SELECT CAST(scope_identity() AS int)";
        using (var insertCommand = new SqlCommand(cmd, con)) {
            insertCommand.Parameters.AddWithValue("@Value", "bar");
            con.Open();
            newID = (int)insertCommand.ExecuteScalar();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Using Visual Studio 2008, and Silverlight 3) -I've really looked around for this one,
Using Visual Studio 2010, I would like to switch over to using InstallShield LE
When using Visual Studio (though ideally this can apply to the generic case) and
Using Visual Studio 2008, I keep seeing this error in the output window: _CrtDbgReport:
I'm using Visual Studio to record some web tests. I'd like the test to
Using visual studio 2008 SP1, This line: LINK : debug\XXXXX.exe not found or not
Using Visual Studio 2008 Team Edition, is it possible to assign a shortcut key
Using Visual Studio 2008 / C# / VS Unit Testing. I have a very
Using Visual Studio .NET 2008 or 2005, is there a way to automatically generate
Using Visual Studio 2008, I created a C++ Win32 project. To release the program,

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.