1
//
在数据库中新建数据表
2 string MySQL = " IF OBJECT_ID(N'MyTest..TableName', N'U') IS NOT NULL " +
3 " DROP TABLE TableName; " +
4 " CREATE TABLE TableName ( " +
5 " [ID] [char] (14) NOT NULL Primary Key, " +
6 " [Name] [char] (14) NULL , " +
7 " [Sex] [char] (40) NULL, " +
8 " [Age] [varchar] (80) NULL , " +
9 " [Tel] [varchar] (40) NULL , " +
10 " [Address] [char] (6) NOT NULL , " +
11 " [Money1] [decimal] (28,6) NULL default(0), " +
12 " [Money2] [decimal] (28,6) NULL default(0), " +
13 " [Money3] [decimal] (28,6) NULL default(0)) " ;
14 SqlConnection MyConnection = new SqlConnection( " Data Source = .;Database = MyTestDb;Integrated Security=SSPI " );
15 SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
16 try
17 {
18 MyCommand.Connection.Open();
19 MyCommand.ExecuteNonQuery();
20 MessageBox.Show("成功在MyTestDb数据库中创建数据表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
21 }
22 catch (Exception ex)
23 {
24 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
25 }
26 finally
27 {
28 MyConnection.Close();
29 }
2 string MySQL = " IF OBJECT_ID(N'MyTest..TableName', N'U') IS NOT NULL " +
3 " DROP TABLE TableName; " +
4 " CREATE TABLE TableName ( " +
5 " [ID] [char] (14) NOT NULL Primary Key, " +
6 " [Name] [char] (14) NULL , " +
7 " [Sex] [char] (40) NULL, " +
8 " [Age] [varchar] (80) NULL , " +
9 " [Tel] [varchar] (40) NULL , " +
10 " [Address] [char] (6) NOT NULL , " +
11 " [Money1] [decimal] (28,6) NULL default(0), " +
12 " [Money2] [decimal] (28,6) NULL default(0), " +
13 " [Money3] [decimal] (28,6) NULL default(0)) " ;
14 SqlConnection MyConnection = new SqlConnection( " Data Source = .;Database = MyTestDb;Integrated Security=SSPI " );
15 SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
16 try
17 {
18 MyCommand.Connection.Open();
19 MyCommand.ExecuteNonQuery();
20 MessageBox.Show("成功在MyTestDb数据库中创建数据表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
21 }
22 catch (Exception ex)
23 {
24 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
25 }
26 finally
27 {
28 MyConnection.Close();
29 }
1
//
在数据库中删除数据表
2 string MySQL = " IF OBJECT_ID(N'MyTestDb..TableName', N'U') IS NOT NULL " +
3 " DROP TABLE TableName; " ;
4 SqlConnection MyConnection = new SqlConnection( " Data Source = .;Database = MyTestDb;Integrated Security=SSPI " );
5 SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
6 try
7 {
8 MyCommand.Connection.Open();
9 MyCommand.ExecuteNonQuery();
10 MessageBox.Show("成功在MyTestDb数据库中删除数据表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
11 }
12 catch (Exception ex)
13 {
14 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
15 }
16 finally
17 {
18 MyConnection.Close();
19 }
2 string MySQL = " IF OBJECT_ID(N'MyTestDb..TableName', N'U') IS NOT NULL " +
3 " DROP TABLE TableName; " ;
4 SqlConnection MyConnection = new SqlConnection( " Data Source = .;Database = MyTestDb;Integrated Security=SSPI " );
5 SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
6 try
7 {
8 MyCommand.Connection.Open();
9 MyCommand.ExecuteNonQuery();
10 MessageBox.Show("成功在MyTestDb数据库中删除数据表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
11 }
12 catch (Exception ex)
13 {
14 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
15 }
16 finally
17 {
18 MyConnection.Close();
19 }