C# :创建SQL Server数据库、设置SQL Server数据库为只读状态、修改和压缩SQL Server数据库、新建(删除和修改)数据表、修改(新增和删除)数据列
2009-09-01 10:49
537 查看
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();
}
}
}
}
相关文章推荐
- 我的XMLHelper----C# XML操作基类(修改,删除,新增,创建)
- C#操作文件、文件夹(复制、删除、新增、设置只读)-技术&分享
- sql server 2005 中数据库表 的创建 ,查看表 属性,表的名称重命名,情况表中信息,删除表 ; 表中列重命名,添加新列,修改列的数据类型,删除列,设置列的主键外键
- sqlite数据类型、关键词及创建、修改、删除数据表
- MySql---数据库和数据表的创建、修改及删除
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- 如何用C#创建、删除、修改文件夹?
- c# 注册表操作,创建,删除,修改,判断节点是否存在
- SQL SERVER 系列 (3)数据表的创建、修改和删除
- Asp.Net XML操作基类(修改,删除,新增,创建)
- jsTree基本使用(新增,修改,删除,移动,点击,加载默认选中根节点,异步加载数据)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- GridView动态新增行 删除行 适合多数据批量修改保存 新增数据验证
- C#实现SQL数据库中的表的查询、添加、修改、删除数据
- 【树莓派开发-Linux】linux创建用户、设置密码、修改用户、删除用户
- linux创建用户、设置密码、修改用户、删除用户:
- 【c#】关于设置RichTextBox背景透明+无光标只读状态
- 数据库、数据表创建后 添加,修改,删除表的列,约束等表的定义。
- Linux创建用户、设置密码、修改用户、删除用户命令
- 转载:怎样在C#里对xml文件进行修改,新增,删除,插入操作?