Export (0) Print
Expand All

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
)

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.
tableInfo
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();
}
NoteNote:

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.

NoteNote:

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();
                try
                {
                    // Begin the SQL statement.
                    createTableText.Append("CREATE TABLE ");
                    // Append the table name to the SQL statement.
                    createTableText.Append(EscapeName(tableInfo.Name));
                    // Begin the list of columns.
                    createTableText.Append(" (");
                    foreach (Column column in tableInfo.Columns)
                    {
                        // Append a column name to the SQL statement.
                        createTableText.Append(EscapeName(column.Name));
                        createTableText.Append(" ");
                        // Append a column type to the SQL statement.
                        createTableText.Append(column.TypeInfo);
                        // 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.
                    createTableText.Append(")");
                    // Create an OLEDB command object.
                    using (OleDbCommand command = new OleDbCommand(createTableText.ToString(), connection))
                    {
                        // Open the connection to the database.
                        connection.Open();
                        // Run the SQL statement.
                        command.ExecuteNonQuery();
                    }
                }
                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.
            escapedName.Append('[');
            // Loop through the string.
            for (int i = 0; i < name.Length; i++)
            {
                char c = name[i];
                if (c == ']')
                {
                    escapedName.Append(']');
                }
                escapedName.Append(c);
            }
            // Append the string with an opening bracket.
            escapedName.Append(']');
            // Return the escaped string.
            return escapedName.ToString();
        }



Show:
© 2014 Microsoft