using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{//創建SQL Server數據庫
string MySQL = "use master;" +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"DROP DATABASE MyDatabase;" +
"CREATE DATABASE MyDatabase " +
"ON(NAME=MyDatabase_dat,FILENAME=\"C:\\MyDatabase.mdf\",SIZE=5,MAXSIZE=10,FILEGROWTH=1) " +
"LOG ON(NAME=MyDatabase_log,FILENAME=\"C:\\MyDatabase.ldf\",SIZE=2,MAXSIZE=5,FILEGROWTH=1)";
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功創建數據庫", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{//設置SQL Server數據庫為只讀狀態
string MySQL = "use master; " +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"EXEC sp_dboption 'MyDatabase', 'read only', 'TRUE'";
// "EXEC sp_dboption 'MyDatabase', 'read only', 'FALSE'";
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("設置MyDatabase數據庫為只讀狀態操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{//設置SQL Server數據庫為脫機狀態
string MySQL = "use master; " +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"EXEC sp_dboption 'MyDatabase', 'offline', 'TRUE'";
// "EXEC sp_dboption 'MyDatabase', 'offline', 'false'";
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("設置MyDatabase數據庫為脫機狀態操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{//修改SQL Server數據庫
string MySQL = "use master;" +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"ALTER DATABASE MyDatabase MODIFY FILE(NAME=MyDatabase_dat,SIZE=20)";
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功修改數據庫的文件尺寸", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{//壓縮SQL Server數據庫
string MySQL = "use master;" +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"DBCC SHRINKDATABASE (MyDatabase, 90) ";
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功壓縮數據庫", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button6_Click(object sender, EventArgs e)
{//在數據庫中新建數據表
string MySQL ="IF OBJECT_ID(N'MyDatabase..商品清單', N'U') IS NOT NULL "+
"DROP TABLE 商品清單;" +
"CREATE TABLE 商品清單 ("+
"[貨號] [char] (14) NOT NULL Primary Key,"+
"[條碼] [char] (14) NULL ,"+
"[拼音編碼] [char] (40) NULL,"+
"[品名] [varchar] (80) NULL ,"+
"[規格] [varchar] (40) NULL ,"+
"[單位] [char] (6) NOT NULL ,"+
"[產地] [varchar] (50) NULL ,"+
"[類別] [char] (20) NULL ,"+
"[進貨價] [decimal] (28,6) NULL default(0),"+
"[銷售價1] [decimal] (28,6) NULL default(0),"+
"[銷售價2] [decimal] (28,6) NULL default(0),"+
"[最低售價] [decimal] (28,6) NULL default(0))";
SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功在MyDatabase數據庫中創建數據表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button7_Click(object sender, EventArgs e)
{//在數據庫中刪除數據表
string MySQL = "IF OBJECT_ID(N'MyDatabase..商品清單', N'U') IS NOT NULL " +
"DROP TABLE 商品清單;";
SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功在MyDatabase數據庫中刪除數據表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button8_Click(object sender, EventArgs e)
{//在數據表中修改數據列
//"[產地] [varchar] (50) NULL ,"
string MySQL = "ALTER TABLE 商品清單 ALTER COLUMN [產地] [char](100) NOT NULL;";
SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功在“商品清單”數據表中修改數據列", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button9_Click(object sender, EventArgs e)
{//在數據表中添加數據列
string MySQL = "ALTER TABLE 商品清單 ADD [檢驗員] [varchar] (50) NULL;";
SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功在“商品清單”數據表中添加數據列", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button10_Click(object sender, EventArgs e)
{//在數據表中刪除數據列
string MySQL = "ALTER TABLE 商品清單 DROP COLUMN [檢驗員] ;";
SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功在“商品清單”數據表中刪除數據列", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
private void button11_Click(object sender, EventArgs e)
{//刪除指定數據表中的所有記錄
string MySQL = "TRUNCATE TABLE 商品清單;";
SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功在“MyDatabase”數據庫中刪除“商品清單”數據表的所有記錄", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
MyConnection.Close();
}
}
}
}