Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. New DataRow in SQL-Server with ID

New DataRow in SQL-Server with ID

Scheduled Pinned Locked Moved C#
databasecsharphelpsql-serversysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I want to insert a row in a table which has a PK in the first field. I make a INSERT and give parameters to the sql server for all fields except the id field which is set in the db as Autoincrement so i dont have to worry about. ALL of this is working fine. But now i want to work immediatly after the insert with the new id of this row, which was given to the row automaticly by the sql-server. My problem now is that i have no Idea how to get the id value into my code (sure I can make a select query questioning all the parameters which i still have from the insertcommand, and ask for the id but this way seems very complicated to me!!!) So if any more experienced Programmer than me can help me, please!!! 10000 Thanks in advanced. PS Languages C#, ADO.Net, and Sql Server 2000 best regards helli

    H D 2 Replies Last reply
    0
    • A Anonymous

      I want to insert a row in a table which has a PK in the first field. I make a INSERT and give parameters to the sql server for all fields except the id field which is set in the db as Autoincrement so i dont have to worry about. ALL of this is working fine. But now i want to work immediatly after the insert with the new id of this row, which was given to the row automaticly by the sql-server. My problem now is that i have no Idea how to get the id value into my code (sure I can make a select query questioning all the parameters which i still have from the insertcommand, and ask for the id but this way seems very complicated to me!!!) So if any more experienced Programmer than me can help me, please!!! 10000 Thanks in advanced. PS Languages C#, ADO.Net, and Sql Server 2000 best regards helli

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Typically, you add a SELECT statement after your INSERT statement when used with a SqlDataAdapter (fills a DataSet from a SQL Server database using Fill, or vice versa using Update). This makes sure that your DataSet is filled with what SQL Server assigned to the PK field. To include both an INSERT and SELECT, separate your statements with a semi-colon (;):

      sqlDataAdapter1.InsertCommand = new SqlCommand(
      "INSERT INTO MyTable Name VALUES (@Name); SELECT ID, Name FROM MyTable",
      sqlConnection1);

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • A Anonymous

        I want to insert a row in a table which has a PK in the first field. I make a INSERT and give parameters to the sql server for all fields except the id field which is set in the db as Autoincrement so i dont have to worry about. ALL of this is working fine. But now i want to work immediatly after the insert with the new id of this row, which was given to the row automaticly by the sql-server. My problem now is that i have no Idea how to get the id value into my code (sure I can make a select query questioning all the parameters which i still have from the insertcommand, and ask for the id but this way seems very complicated to me!!!) So if any more experienced Programmer than me can help me, please!!! 10000 Thanks in advanced. PS Languages C#, ADO.Net, and Sql Server 2000 best regards helli

        D Offline
        D Offline
        denizmercan
        wrote on last edited by
        #3

        I think you need to use "IDENTITY/IDENT_CURRENT or SCOPE_IDENTITY" method of SQL. But I don't know how you can use it. If you get full source code please let me know.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups