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. Database & SysAdmin
  3. Database
  4. SQL SMO Fast way to get primary key column from a table

SQL SMO Fast way to get primary key column from a table

Scheduled Pinned Locked Moved Database
databasequestion
5 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.
  • P Offline
    P Offline
    Polymorpher
    wrote on last edited by
    #1

    Does anyone know a fast way of getting the primary key column of a table using SQL SMO? My aproach so far has been to run through all of the columns checking to see if they are primary key columns...

    Apparently it's not OK to start a bonfire of Microsoft products in the aisles of CompUSA even though the Linuxrulz web site says so

    S O 2 Replies Last reply
    0
    • P Polymorpher

      Does anyone know a fast way of getting the primary key column of a table using SQL SMO? My aproach so far has been to run through all of the columns checking to see if they are primary key columns...

      Apparently it's not OK to start a bonfire of Microsoft products in the aisles of CompUSA even though the Linuxrulz web site says so

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      Get it[^]

      Regards, Satips.:rose:

      P 1 Reply Last reply
      0
      • S Sathesh Sakthivel

        Get it[^]

        Regards, Satips.:rose:

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

        tbl.Indexes(0).Name seems to give me the constraint name...but i need to column name, sorry if i missed it, but i didnt see it in your code. Thanks for the quick response :)

        Apparently it's not OK to start a bonfire of Microsoft products in the aisles of CompUSA even though the Linuxrulz web site says so

        1 Reply Last reply
        0
        • P Polymorpher

          Does anyone know a fast way of getting the primary key column of a table using SQL SMO? My aproach so far has been to run through all of the columns checking to see if they are primary key columns...

          Apparently it's not OK to start a bonfire of Microsoft products in the aisles of CompUSA even though the Linuxrulz web site says so

          O Offline
          O Offline
          originSH
          wrote on last edited by
          #4

          As far as I can remember you'd have to loop and check. Is the problem that doign the loop and check is very slow? If so check out the 'SetDefaultInitFields' method.

          P 1 Reply Last reply
          0
          • O originSH

            As far as I can remember you'd have to loop and check. Is the problem that doign the loop and check is very slow? If so check out the 'SetDefaultInitFields' method.

            P Offline
            P Offline
            Polymorpher
            wrote on last edited by
            #5

            The fastest way i have found so far is by extending your idea slightly: If tbl.Indexes.Count > 0 Then If tbl.Indexes(0).IndexedColumns.Count > 0 Then Return tbl.Indexes(0).IndexedColumns(0).Name End If End If

            Apparently it's not OK to start a bonfire of Microsoft products in the aisles of CompUSA even though the Linuxrulz web site says so

            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