用c#创建mssql数据库
作者:admin 日期:2007-05-17
[em02]其实是很简单的事啦,发出来给大家,有需要的时候可以用一用.
using System.data.sqlclient;
private void CreateDatabase(DatabaseParameter DBParam)
{
System.Data.SqlClient.SqlConnection DBConn;
string sqlCreateDBQuery;
DBConn = new SqlConnection();
DBConn.ConnectionString = "SERVER = " + DBParam.ServerName +
"; DATABASE = master; User ID = dezai; Pwd = sa";
sqlCreateDBQuery = " CREATE DATABASE "
+ DBParam.DatabaseName
+ " ON PRIMARY "
+ " (NAME = " + DBParam.DataFileName +", "
+ " FILENAME = '" + DBParam.DataPathName +"', "
+ " SIZE = 2MB,"
+ " FILEGROWTH =" + DBParam.DataFileGrowth +") "
+ " LOG ON (NAME =" + DBParam.LogFileName +", "
+ " FILENAME = '" + DBParam.LogPathName + "', "
+ " SIZE = 1MB, "
+ " FILEGROWTH =" + DBParam.LogFileGrowth +") ";
SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, DBConn);
try
{
DBConn.Open();
MessageBox.Show(sqlCreateDBQuery);
myCommand.ExecuteNonQuery();
MessageBox.Show("创建成功!",
"Create Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Create Database",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
finally
{
DBConn.Close();
}
return;
}
DatabaseParameter 是自定义的一个class object,里面包含的属性有
DBParam.ServerName :SQL服务器名称;
DBParam.DataFileName:要创建数据库的文件名
DBParam.DataFileGrowth :数据库文件自动增长的百分比
DBParam.LogFileName:要创建数据库的日志的文件名
DBParam.LogPathName:要创建数据库的日志的路径
DBParam.LogFileGrowth: 数据库日志自动增长的百分比
这个类在sql2000下测试OK,没有在sql2005试过,不过大家一看应该很清楚其中的原理了。
using System.data.sqlclient;
private void CreateDatabase(DatabaseParameter DBParam)
{
System.Data.SqlClient.SqlConnection DBConn;
string sqlCreateDBQuery;
DBConn = new SqlConnection();
DBConn.ConnectionString = "SERVER = " + DBParam.ServerName +
"; DATABASE = master; User ID = dezai; Pwd = sa";
sqlCreateDBQuery = " CREATE DATABASE "
+ DBParam.DatabaseName
+ " ON PRIMARY "
+ " (NAME = " + DBParam.DataFileName +", "
+ " FILENAME = '" + DBParam.DataPathName +"', "
+ " SIZE = 2MB,"
+ " FILEGROWTH =" + DBParam.DataFileGrowth +") "
+ " LOG ON (NAME =" + DBParam.LogFileName +", "
+ " FILENAME = '" + DBParam.LogPathName + "', "
+ " SIZE = 1MB, "
+ " FILEGROWTH =" + DBParam.LogFileGrowth +") ";
SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, DBConn);
try
{
DBConn.Open();
MessageBox.Show(sqlCreateDBQuery);
myCommand.ExecuteNonQuery();
MessageBox.Show("创建成功!",
"Create Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Create Database",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
finally
{
DBConn.Close();
}
return;
}
DatabaseParameter 是自定义的一个class object,里面包含的属性有
DBParam.ServerName :SQL服务器名称;
DBParam.DataFileName:要创建数据库的文件名
DBParam.DataFileGrowth :数据库文件自动增长的百分比
DBParam.LogFileName:要创建数据库的日志的文件名
DBParam.LogPathName:要创建数据库的日志的路径
DBParam.LogFileGrowth: 数据库日志自动增长的百分比
这个类在sql2000下测试OK,没有在sql2005试过,不过大家一看应该很清楚其中的原理了。
评论: 0 | 引用: 1 | 查看次数: 18889
发表评论