Sql 代码
CREATE DATABASE [StudentDB] CONTAINMENT = NONE ON
PRIMARY ( NAME = N'StudentDB', FILENAME = N'D:\Demo\DBs\StudentDB.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON ( NAME = N'StudentDB_log', FILENAME = N'G:\Demo\DBs\StudentDB_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
C# 实现将数据库创建到指定目录的两种方式
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="DBName">数据库名称</param>
/// <param name="DBpath">数据库位置</param>
/// <returns>返回:sql语句</returns>
public string CreateDB(string DBName, string DBpath)
{
string sql = string.Format(@"
CREATE DATABASE [{0}] CONTAINMENT = NONE ON
PRIMARY ( NAME = N'{0}', FILENAME = N'{1}\{0}.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON ( NAME = N'{0}_log', FILENAME = N'{1}\{0}_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )", DBName, DBpath);
return sql;
}
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="DBName">数据库名称</param>
/// <returns>返回:sql语句</returns>
public string CreateDB(string DBName)
{
string DBpath;
try
{
//引用 System.Windows.Forms;
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
//在树视图控件上显示的说明文本
folderBrowserDialog.Description = "选择文件夹位置";
DialogResult dialogResult = folderBrowserDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
DBpath = folderBrowserDialog.SelectedPath;
string sql = string.Format(@"
CREATE DATABASE [{0}] CONTAINMENT = NONE ON
PRIMARY ( NAME = N'{0}', FILENAME = N'{1}\{0}.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON ( NAME = N'{0}_log', FILENAME = N'{1}\{0}_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )", DBName, DBpath);
//Console.WriteLine("选择的文件夹位置: " + DBpath);
return sql;
}
else
{
return "";
}
}
catch (Exception ex)
{
return "";
throw new Exception(ex.Message);
}
}