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. database

database

Scheduled Pinned Locked Moved C#
questioncsharpdatabasehelp
6 Posts 5 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.
  • F Offline
    F Offline
    farokhian
    wrote on last edited by
    #1

    Hi I need somebody help. In my c# project i need primary key of the row that i insert into my table; what should i do? If it is necessary to write storedprocedure , how can i use it in my project?

    C P 2 Replies Last reply
    0
    • F farokhian

      Hi I need somebody help. In my c# project i need primary key of the row that i insert into my table; what should i do? If it is necessary to write storedprocedure , how can i use it in my project?

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      Use a statment like this: string szStatement = "SET NOCOUNT ON; INSERT INTO tablename (column_1, column_2, ..., column_n) VALUES(?,?, ..., ?); SELECT @@IDENTITY AS ID;"; someDatabaseCommand = new SomeDatabaseCommand(dbConnection, szStatement); int nNewId = (int)someDatabaseCommand.ExecuteScalar();

      Greetings Covean

      1 Reply Last reply
      0
      • F farokhian

        Hi I need somebody help. In my c# project i need primary key of the row that i insert into my table; what should i do? If it is necessary to write storedprocedure , how can i use it in my project?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I use GUIDs as primary keys. When I need the key of a row I'm about to insert I create the GUID in the application with System.Guid.NewGuid() and then pass it in with the rest of the data. I never use autoincrement fields; they're so last century.

        F 1 Reply Last reply
        0
        • P PIEBALDconsult

          I use GUIDs as primary keys. When I need the key of a row I'm about to insert I create the GUID in the application with System.Guid.NewGuid() and then pass it in with the rest of the data. I never use autoincrement fields; they're so last century.

          F Offline
          F Offline
          farokhian
          wrote on last edited by
          #4

          Thanks alot. Can you explain me that what is GUIDs? How can i use it? should i create it c# code?

          S H 2 Replies Last reply
          0
          • F farokhian

            Thanks alot. Can you explain me that what is GUIDs? How can i use it? should i create it c# code?

            S Offline
            S Offline
            Saksida Bojan
            wrote on last edited by
            #5

            farokhian wrote:

            Can you explain me that what is GUIDs?

            Globally Unique Identifier[^]

            farokhian wrote:

            should i create it c# code?

            All you will need is to call Managed Call to create GUID

            farokhian wrote:

            How can i use it?

            Guid.NewGUID().ToString() Al thou I Don't recommend this approach for database. The process is very slow, and there you can get duplicates GUIDs. I Recommend using Integer ID, With Auto_Increment options. When inserting Row, in SQL command do not specify column ID and value. It will grammatically give free ID number

            1 Reply Last reply
            0
            • F farokhian

              Thanks alot. Can you explain me that what is GUIDs? How can i use it? should i create it c# code?

              H Offline
              H Offline
              Hawks Talon
              wrote on last edited by
              #6

              GUIDs a string values used for uniqueness (example: 33b61dbd-2640-4153-8556-9cb9ad3e4a71). They are great for ensuring a unique identifier but not so great for indexing as they are string and thus cause the DB to have to do a table scan in order sort the records properly. Indentity or auto-increment field types may be old-school but they have the advantage of not needing to scan the table for ordering and thus provide much better performance for indexing. This is important because the Primary Key is an Index, the primary index in fact. The main advantage a GUID provides is with database farms. A record with a GUID will always have the same GUID but the Indentity could get reset across servers if precausons are not taken. All that being said, I feel your original question was not answered. In order to answer your question though I would need a little bit more info as the answer could differ. How are you connecting to the DB? ADO.Net? Linq? What kind of database are you connecting to? Aceess? SQL? MySQL? Thank you, Eric Ritzie

              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