Can I create a primary key ? RRS feed

  • Question

  • Hi;

    Can I add a primary key as part of this line :

     aDataTable.Columns.Add("ID", GetType(Int32))
    Here is how I am creating my datatable structure :
    <pre lang="x-vbnet">Dim tblColsConn As SqlConnection = New SqlConnection(tblColsConnStr)
          Dim tblColsStrSQL As String = "Select ID, Title from currentSystems"
          Dim tblColCmd As New SqlCommand(tblColsStrSQL, tblColsConn)
          aDataTable.Columns.Add("ID", GetType(Int32))
          aDataTable.Columns.Add("Officer", GetType(String))
          Dim aRdr As SqlDataReader = tblColCmd.ExecuteReader
          Do While aRdr.Read
            aDataTable.Columns.Add(aRdr.Item(1).ToString, GetType(Int32)).AllowDBNull = True
            colCount = colCount + 1
          aDataTable.Columns.Add("Totals", GetType(Int32))

    Thanks for your insights !


    • Moved by VMazurModerator Thursday, July 7, 2011 10:31 AM (From:ADO.NET Managed Providers)
    Wednesday, July 6, 2011 3:37 PM


  • Thanks anyway I found and example in Sceppa's book chap 6 - Working with dataset objects.

    Here is is :

    aDataTable.Columns.Add("OfficerID", GetType(Int32)) 
     aDataTable.PrimaryKey = New DataColumn() {aDataTable.Columns("OfficerID")}



    • Marked as answer by GeeMann Wednesday, July 6, 2011 3:50 PM
    Wednesday, July 6, 2011 3:49 PM