tailieunhanh - Creating a New SQL Server Database

[ Team LiB ] Recipe Creating a New SQL Server Database Problem You need to create a new database in your SQL Server. Solution Use the CREATE DATABASE statement. The sample code executes the DDL statement—using the ExecuteNonQuery( ) | Team LiB Recipe Creating a New SQL Server Database Problem You need to create a new database in your SQL Server. Solution Use the CREATE DATABASE statement. The sample code executes the DDL statement using the ExecuteNonQuery method of the Command object to create a new database named MyDatabase in SQL Server. The C code is shown in Example 10-7. Example 10-7. File Namespaces variables and constants using System using using using using . . . StringBuilder sb new StringBuilder SQL DDL command text to create database. String sqlText CREATE DATABASE MyDatabase ON PRIMARY NAME MyDatabase_Data FILENAME DATAFILENAME SIZE 2MB MAXSIZE 10MB FILEGROWTH 10 LOG ON NAME MyDatabase_Log FILENAME LOGFILENAME SIZE 1MB MAXSIZE 5MB FILEGROWTH 10 sqlText Create a connection. SqlConnection conn new SqlConnection Sql_Master_ConnectString Create the command to create the database. SqlCommand cmd new SqlCommand sqlText conn Create the new database. try DataBase created successfully. catch ex finally if Discussion There are two categories of SQL statements Database Definition Language DDL Used to manage all objects in the database generally with CREATE ALTER and DROP statements to create modify and delete objects respectively. These statements generally require DBA permissions to execute. Database Management Language DML Used to manipulate select insert update and delete data in the database objects. Database objects are defined using DDL. The solution executes a DDL CREATE DATABASE statement to create a new database on a SQL Server. You can programmatically drop the database by using the DROP DATABASE statement in a similar way. To drop the .