SqlServer 数据库创建到指定目录

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);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值