Adding Tables to a Database
Số trang: 3
Loại file: pdf
Dung lượng: 14.00 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.8 Adding Tables to a Database Problem You need to add a table to an existing database. Solution Use the CREATE TABLE statement. The sample code executes the DDL statement—using the ExecuteNonQuery( ) method of the Command object—to add a table to an existing SQL Server database.
Nội dung trích xuất từ tài liệu:
Adding Tables to a Database[ Team LiB ]Recipe 10.8 Adding Tables to a DatabaseProblemYou need to add a table to an existing database.SolutionUse the CREATE TABLE statement.The sample code executes the DDL statement—using the ExecuteNonQuery( ) method ofthe Command object—to add a table to an existing SQL Server database.The C# code is shown in Example 10-8.Example 10-8. File: AddTableToDatabaseForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;// . . .SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]);String createSql = CREATE TABLE MyTable + (MyTableId int IDENTITY(1,1) PRIMARY KEY CLUSTERED);SqlCommand cmd = new SqlCommand(createSql, conn);// Create the table in the database.try{ conn.Open( ); cmd.ExecuteNonQuery( ); resultTextBox.Text = Table created successfully;}catch (System.Exception ex){ resultTextBox.Text = ex.ToString( );}finally{ if (conn.State == ConnectionState.Open) conn.Close( );}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 TABLE statement to create a table in thedatabase and a primary key on the new table in a SQL Server database.You can programmatically drop a table using the DROP TABLE statement in a similarway. To drop the table created in this example, use the following code:DROP TABLE MyTableThe DROP TABLE statement will fail if the table is in use; therefore, it might benecessary to restart the SQL Server.For more information about the CREATE TABLE statement or the DROP TABLEstatement, see Microsoft SQL Server Books Online.The solution for Oracle databases and other databases is similar to that shown for SQLServer. However, the DDL syntax for each database varies slightly because of differencesin database server capabilities and architecture. For example, the CREATE TABLEstatement for Oracle is different because Oracle does not support identity columns anduses sequences instead (see Recipe 4.4 for more information about Oracle sequences).For more information about Oracle SQL syntax, see Oracle in a Nutshell by RickGreenwald and David C. Kreines (OReilly).[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Adding Tables to a Database[ Team LiB ]Recipe 10.8 Adding Tables to a DatabaseProblemYou need to add a table to an existing database.SolutionUse the CREATE TABLE statement.The sample code executes the DDL statement—using the ExecuteNonQuery( ) method ofthe Command object—to add a table to an existing SQL Server database.The C# code is shown in Example 10-8.Example 10-8. File: AddTableToDatabaseForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;// . . .SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]);String createSql = CREATE TABLE MyTable + (MyTableId int IDENTITY(1,1) PRIMARY KEY CLUSTERED);SqlCommand cmd = new SqlCommand(createSql, conn);// Create the table in the database.try{ conn.Open( ); cmd.ExecuteNonQuery( ); resultTextBox.Text = Table created successfully;}catch (System.Exception ex){ resultTextBox.Text = ex.ToString( );}finally{ if (conn.State == ConnectionState.Open) conn.Close( );}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 TABLE statement to create a table in thedatabase and a primary key on the new table in a SQL Server database.You can programmatically drop a table using the DROP TABLE statement in a similarway. To drop the table created in this example, use the following code:DROP TABLE MyTableThe DROP TABLE statement will fail if the table is in use; therefore, it might benecessary to restart the SQL Server.For more information about the CREATE TABLE statement or the DROP TABLEstatement, see Microsoft SQL Server Books Online.The solution for Oracle databases and other databases is similar to that shown for SQLServer. However, the DDL syntax for each database varies slightly because of differencesin database server capabilities and architecture. For example, the CREATE TABLEstatement for Oracle is different because Oracle does not support identity columns anduses sequences instead (see Recipe 4.4 for more information about Oracle sequences).For more information about Oracle SQL syntax, see Oracle in a Nutshell by RickGreenwald and David C. Kreines (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 Adding Tables to a 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