Creating a New SQL Server Database
Số trang: 3
Loại file: pdf
Dung lượng: 14.40 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 10.7 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( )
Nội dung trích xuất từ tài liệu:
Creating a New SQL Server Database [ Team LiB ]Recipe 10.7 Creating a New SQL Server DatabaseProblemYou need to create a new database in your SQL Server.SolutionUse the CREATE DATABASE statement.The sample code executes the DDL statement—using the ExecuteNonQuery( ) method ofthe 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: CreateServerDatabaseForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// . . .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%);sb.Append(sqlText + Environment.NewLine + Environment.NewLine);// Create a connection.SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_Master_ConnectString]);// Create the command to create the database.SqlCommand cmd = new SqlCommand(sqlText, conn);// Create the new database.try{ conn.Open( ); cmd.ExecuteNonQuery( ); sb.Append(DataBase created successfully.);}catch (System.Exception ex){ sb.Append(ex.ToString( ));}finally{ if (conn.State == ConnectionState.Open) conn.Close( ); conn.Close( );}resultTextBox.Text = sb.ToString( );DiscussionThere 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 databaseon a SQL Server.You can programmatically drop the database by using the DROP DATABASE statementin a similar way. To drop the database created in the previous example, use the followingcode:DROP DATABASE MyDatabaseThe DROP DATABASE statement will fail if the database is in use; therefore, it mightbe necessary to restart the SQL Server in order to drop the database. System databases—master, model, msdb, and tempdb—cannot be dropped.For more information about the CREATE DATABASE statement, the DROPDATABASE statement, or DDL, see Microsoft SQL Server Books Online.The solution for Oracle databases and other databases is similar to that shown for SQLServer although the DDL syntax for each database varies slightly because of differencesin database server capabilities and architecture. For more information about Oracle SQLsyntax, see Oracle in a Nutshell (OReilly).[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Creating a New SQL Server Database [ Team LiB ]Recipe 10.7 Creating a New SQL Server DatabaseProblemYou need to create a new database in your SQL Server.SolutionUse the CREATE DATABASE statement.The sample code executes the DDL statement—using the ExecuteNonQuery( ) method ofthe 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: CreateServerDatabaseForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// . . .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%);sb.Append(sqlText + Environment.NewLine + Environment.NewLine);// Create a connection.SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_Master_ConnectString]);// Create the command to create the database.SqlCommand cmd = new SqlCommand(sqlText, conn);// Create the new database.try{ conn.Open( ); cmd.ExecuteNonQuery( ); sb.Append(DataBase created successfully.);}catch (System.Exception ex){ sb.Append(ex.ToString( ));}finally{ if (conn.State == ConnectionState.Open) conn.Close( ); conn.Close( );}resultTextBox.Text = sb.ToString( );DiscussionThere 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 databaseon a SQL Server.You can programmatically drop the database by using the DROP DATABASE statementin a similar way. To drop the database created in the previous example, use the followingcode:DROP DATABASE MyDatabaseThe DROP DATABASE statement will fail if the database is in use; therefore, it mightbe necessary to restart the SQL Server in order to drop the database. System databases—master, model, msdb, and tempdb—cannot be dropped.For more information about the CREATE DATABASE statement, the DROPDATABASE statement, or DDL, see Microsoft SQL Server Books Online.The solution for Oracle databases and other databases is similar to that shown for SQLServer although the DDL syntax for each database varies slightly because of differencesin database server capabilities and architecture. For more information about Oracle SQLsyntax, see Oracle in a Nutshell (OReilly).[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Creating a New SQL Server DatabaseTài liệu liên quan:
-
52 trang 434 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 293 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0