IDbTableManager.GetPrimaryKey Method

IIS 7.0

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)

string[] GetPrimaryKey(
	string connectionString,
	string tableName,
	string schema
)

Parameters

connectionString
Type: System.String
The connection string for the database.
tableName
Type: System.String
The name of the table.
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: System.String[]
The array of primary keys.

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();
}

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.



        // 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();
        }



Show: