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. Visual Basic
  4. can i retrieve the primary keys of a table?

can i retrieve the primary keys of a table?

Scheduled Pinned Locked Moved Visual Basic
databasexmlhelpquestion
7 Posts 2 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.
  • S Offline
    S Offline
    sumit21
    wrote on last edited by
    #1

    i want to retrieve of a table whose schema and other constraints i do not know basically , i had given user opurtunity to browse the database (and select any table) and based on that i will display the priary keys(if it does not had primey keys then a msgbox will be displayed) is there any method to know wheteher table has primary keys and to bind that column to the combobox plz help a code snippet will be more helpfull....

    N 1 Reply Last reply
    0
    • S sumit21

      i want to retrieve of a table whose schema and other constraints i do not know basically , i had given user opurtunity to browse the database (and select any table) and based on that i will display the priary keys(if it does not had primey keys then a msgbox will be displayed) is there any method to know wheteher table has primary keys and to bind that column to the combobox plz help a code snippet will be more helpfull....

      N Offline
      N Offline
      Nicholas Cardi
      wrote on last edited by
      #2

      You can use the stored proc sp_primarykeys Best of luck Forever Developing

      S 1 Reply Last reply
      0
      • N Nicholas Cardi

        You can use the stored proc sp_primarykeys Best of luck Forever Developing

        S Offline
        S Offline
        sumit21
        wrote on last edited by
        #3

        i m using ms access plz help me code will be very useful

        N 1 Reply Last reply
        0
        • S sumit21

          i m using ms access plz help me code will be very useful

          N Offline
          N Offline
          Nicholas Cardi
          wrote on last edited by
          #4

          I was not sure if you were using OLEDB or ODBC so I did it in OLEDB. Private Sub cmdFindPrimary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFindPrimary.Click Try Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=V:\VBScratchPad\test.mdb;" Dim cn As OleDbConnection = New OleDbConnection(ConnString) cn.Open() Dim da As New OleDbDataAdapter("Select * from Test", cn) Dim ds As New DataSet("TESTING") da.FillSchema(ds, SchemaType.Mapped) For i As Integer = 0 To ds.Tables(0).PrimaryKey.GetUpperBound(0) Console.WriteLine(ds.Tables(0).PrimaryKey(i).ColumnName) Next Catch ex As ApplicationException MessageBox.Show(ex.ToString()) End Try End Sub Best of Luck Forever Developing

          S 1 Reply Last reply
          0
          • N Nicholas Cardi

            I was not sure if you were using OLEDB or ODBC so I did it in OLEDB. Private Sub cmdFindPrimary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFindPrimary.Click Try Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=V:\VBScratchPad\test.mdb;" Dim cn As OleDbConnection = New OleDbConnection(ConnString) cn.Open() Dim da As New OleDbDataAdapter("Select * from Test", cn) Dim ds As New DataSet("TESTING") da.FillSchema(ds, SchemaType.Mapped) For i As Integer = 0 To ds.Tables(0).PrimaryKey.GetUpperBound(0) Console.WriteLine(ds.Tables(0).PrimaryKey(i).ColumnName) Next Catch ex As ApplicationException MessageBox.Show(ex.ToString()) End Try End Sub Best of Luck Forever Developing

            S Offline
            S Offline
            sumit21
            wrote on last edited by
            #5

            i had written the code but i m still getting exceptions here is my code con is oledbconnection adap is oledbdataadapter ds is dataset con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admin;Data Source=D:\Documents and Settings\sumit\My Documents\db1.mdb") adap = New OleDbDataAdapter("select * from table4", con) ds = New DataSet("testing") adap.FillSchema(ds, SchemaType.Mapped) Dim i As Integer Try For i = 0 To ds.Tables("table4").PrimaryKey.GetUpperBound(0) combobox1.items.add(ds.Tables("Table4").PrimaryKey(i).ColumnName()) Next Catch ex As System.Exception MsgBxox(ex.Source + " " + ex.Message + " " + ex.ToString) End Try End Sub and i m getting the exception "object refrence not set to instance of object" help plz

            N 1 Reply Last reply
            0
            • S sumit21

              i had written the code but i m still getting exceptions here is my code con is oledbconnection adap is oledbdataadapter ds is dataset con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admin;Data Source=D:\Documents and Settings\sumit\My Documents\db1.mdb") adap = New OleDbDataAdapter("select * from table4", con) ds = New DataSet("testing") adap.FillSchema(ds, SchemaType.Mapped) Dim i As Integer Try For i = 0 To ds.Tables("table4").PrimaryKey.GetUpperBound(0) combobox1.items.add(ds.Tables("Table4").PrimaryKey(i).ColumnName()) Next Catch ex As System.Exception MsgBxox(ex.Source + " " + ex.Message + " " + ex.ToString) End Try End Sub and i m getting the exception "object refrence not set to instance of object" help plz

              N Offline
              N Offline
              Nicholas Cardi
              wrote on last edited by
              #6

              Not sure why this is but I found a solution to you problem For i = 0 To ds.Tables(0).PrimaryKey.GetUpperBound(0) ComboBox1.Items.Add(ds.Tables(0).PrimaryKey(i).ColumnName()) Next it seems that when filling the schema you cannot refer to the table by its name but only by index number or the value "Table" Forever Developing

              S 1 Reply Last reply
              0
              • N Nicholas Cardi

                Not sure why this is but I found a solution to you problem For i = 0 To ds.Tables(0).PrimaryKey.GetUpperBound(0) ComboBox1.Items.Add(ds.Tables(0).PrimaryKey(i).ColumnName()) Next it seems that when filling the schema you cannot refer to the table by its name but only by index number or the value "Table" Forever Developing

                S Offline
                S Offline
                sumit21
                wrote on last edited by
                #7

                it still not working is there any method top get the name of that column.

                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