IDbTableManager.GetPrimaryKey Method

Returns a list of primary keys for the specified table.

Namespace: Microsoft.Web.Management.DatabaseManager
Assembly: Microsoft.Web.Management.DatabaseManager (in Microsoft.Web.Management.DatabaseManager.dll)

Syntax

'Declaration
Function GetPrimaryKey ( _
    connectionString As String, _
    tableName As String, _
    schema As String _
) As String()
'Usage
Dim instance As IDbTableManager
Dim connectionString As String
Dim tableName As String
Dim schema As String
Dim returnValue As String()

returnValue = instance.GetPrimaryKey(connectionString, _
    tableName, schema)
string[] GetPrimaryKey(
    string connectionString,
    string tableName,
    string schema
)
array<String^>^ GetPrimaryKey(
    String^ connectionString, 
    String^ tableName, 
    String^ schema
)
function GetPrimaryKey(
    connectionString : String, 
    tableName : String, 
    schema : String
) : String[]

Parameters

  • connectionString
    Type: System.String
    The connection string for the database.
  • schema
    Type: System.String
    The schema name for the table.
    Note If schema is empty, the default schema name will be used.

Return Value

Type: array<System.String[]
The array of primary keys.

Remarks

All database providers that implement the IDbTableManager interface must also implement the GetPrimaryKey method, which the database manager will use to retrieve an array of primary keys from a database.

Notes for Implementers

If your provider does not support retrieving the list of primary keys, you can use the following code sample to raise a not-implemented exception:

public string[] GetPrimaryKey(string connectionString, string tableName, string schema)
{
    throw new NotImplementedException();
}

Examples

The following code sample shows how to use the GetPrimaryKey method to retrieve the list of primary keys for an OLEDB connection by using the table name that the database manager provides.


    Public Function GetPrimaryKey( _
            ByVal connectionString As String, _
            ByVal tableName As String, _
            ByVal schema As String) As String() _
            Implements Microsoft.Web.Management.DatabaseManager.IDbTableManager.GetPrimaryKey

        Dim restrictions() As String = New String() {Nothing, Nothing, tableName}
        Dim dataTable As DataTable
        Dim primaryKeys As List(Of String) = New List(Of String)
        ' Create a connection to the database.
        Dim connection As OleDbConnection = New OleDbConnection(connectionString)
        connection.Open()
        ' Open the schema information for the primary keys.
        dataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, restrictions)
        ' Enumerate the table's primary keys.
        For Each row As DataRow In dataTable.Rows
            ' Append each primary key to the list.
            primaryKeys.Add(row("COLUMN_NAME").ToString)
        Next
        ' Return the list of primary keys.
        Return primaryKeys.ToArray

    End Function


        // Retrieve a list of primary keys for a table.
        public string[] GetPrimaryKey(string connectionString, string tableName, string schema)
        {
            String[] restrictions = new string[] { null, null, tableName };
            DataTable dataTable;
            List<string> primaryKeys = new List<string>();

            // Create a connection to the database.
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                connection.Open();
                // Open the schema information for the primary keys.
                dataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, restrictions);
                // Enumerate the table's primary keys.
                foreach (DataRow row in dataTable.Rows)
                {
                    // Append each primary key to the list.
                    primaryKeys.Add(row["COLUMN_NAME"].ToString());
                }
            }
            // Return the list of primary keys.
            return primaryKeys.ToArray();
        }

Permissions

See Also

Reference

IDbTableManager Interface

Microsoft.Web.Management.DatabaseManager Namespace