Beispiel #1
0
        /// <summary>
        /// createDBByCopy: 通过复制模版数据库文件来创建数据库
        /// </summary>
        /// <param name="_DBName">新建数据库名</param>
        /// <param name="_sourcePath">模版数据库名</param>
        /// <param name="_targetPath">新建数据库路径</param>
        /// <param name="_targetPath">模版数据库路径</param>
        public string createDBByCopy(string _DBName, string _DBTemplate, string _targetPath, string _sourcePath)
        {
            string _return = String.Empty;

            try
            {
                string _tPath = _targetPath + "\\" + _DBName, _sPath = _sourcePath + "\\Fily";
                helper.execNonQuery(@"
                                    use master;if exists (select * from sysdatabases where name='{0}') drop database {0};
                                    exec sp_dboption '{1}', 'offline', 'True';alter database {1} set single_user with rollback IMMEDIATE;
                    ", _DBName, _DBTemplate);
                MFile.createFile(_tPath);
                MFile.copy(_sPath + "\\" + _DBTemplate + ".mdf", _tPath + "\\" + _DBName + ".mdf", true);
                MFile.copy(_sPath + "\\" + _DBTemplate + "_log.ldf", _tPath + "\\" + _DBName + "_log.ldf", true);
                helper.execNonQuery(@"
                                    use master; exec sp_dboption '{1}', 'offline', 'False';alter database {1} set multi_user;
                                    CREATE DATABASE {0} ON  (FILENAME = '{2}\\0}.mdf'), (FILENAME = '{2}\\{0}_log.ldf') FOR ATTACH;
                    ", _DBName, _DBTemplate, _tPath);
            }
            catch (Exception ex) {
                _return = Native.getErrorMsg(ex.Message);
            }
            return(_return);
        }