IDbTableManager.CreateTable Method

IIS 7.0

Creates a table in a database.

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

void CreateTable(
	string connectionString,
	string schema,
	TableInfo tableInfo


Type: System.String
The connection string for the database.
Type: System.String
The schema name for the table.
Note   If schema is empty, the default schema name will be used.
Type: Microsoft.Web.Management.DatabaseManager.TableInfo
A TableInfo object that contains the list of information about the table and columns.

All database providers that implement the IDbTableManager interface must also implement the CreateTable method, which the database manager will use to create tables in a database.

Notes for Implementers

If your provider does not support adding tables, you can use the following code sample to raise a not-implemented exception:

public void CreateTable(string connectionString, string schema, TableInfo tableInfo)
   throw new NotImplementedException();

See the CREATE TABLE (Transact-SQL) topic for more information about the CREATE TABLE SQL statement.

The following code sample implements the CreateTable method for an OLEDB connection by using the TableInfo object that the database manager provides.


This example also provides helper methods that perform additional database tasks.

        // Create a table in the database.
        public void CreateTable(
            string connectionString,
            string schema,
            TableInfo tableInfo )
            // Create a connection to the database.
            using (OleDbConnection connection = new OleDbConnection(connectionString))
                // Create a new string builder for the SQL statement.
                StringBuilder createTableText = new StringBuilder();
                    // Begin the SQL statement.
                    createTableText.Append("CREATE TABLE ");
                    // Append the table name to the SQL statement.
                    // Begin the list of columns.
                    createTableText.Append(" (");
                    foreach (Column column in tableInfo.Columns)
                        // Append a column name to the SQL statement.
                        createTableText.Append(" ");
                        // Append a column type to the SQL statement.
                        // Test if the column type allows nulls.
                        createTableText.Append(column.AllowNulls ? " NOT NULL, " : " NULL, ");
                    createTableText.Remove(createTableText.Length - 2, 2);
                    // End the list of columns.
                    // Create an OLEDB command object.
                    using (OleDbCommand command = new OleDbCommand(createTableText.ToString(), connection))
                        // Open the connection to the database.
                        // Run the SQL statement.
                catch(Exception ex)
                    // Raise an exception if an error occurs.
                    throw new ProviderException("CREATE TABLE Exception:\n\n" + ex.Message);


        internal static string EscapeName(string name)
            // Test if the value is already escaped.
            if (name.StartsWith("[", StringComparison.Ordinal) && name.EndsWith("]", StringComparison.Ordinal))
                // Return the string with no changes.
                return name;
            // Create a new string builder for the escaped string.
            StringBuilder escapedName = new StringBuilder();
            // Prepend the string with an opening bracket.
            // Loop through the string.
            for (int i = 0; i < name.Length; i++)
                char c = name[i];
                if (c == ']')
            // Append the string with an opening bracket.
            // Return the escaped string.
            return escapedName.ToString();