Microsoft.SqlServer.Management.Smo   CreateDataBase


public void CreateDataBase(string newDataBaseName, string dbPath)
{
sqlsvr.ConnectionContext.Connect();

var dbFileName = newDataBaseName;
Database nDB = new Database(sqlsvr, newDataBaseName);

DataFile nMdfFile = new DataFile();
LogFile nLogFile = new LogFile();
nMdfFile.Name = newDataBase;
var fileGroup = new FileGroup();
fileGroup.Parent = nDB;
fileGroup.Name = "PRIMARY";
fileGroup.FileGroupType = FileGroupType.RowsFileGroup;

nMdfFile.Parent = fileGroup;
nMdfFile.FileName = dbPath + dbFileName + ".mdf";
nMdfFile.GrowthType = FileGrowthType.None;//?
nMdfFile.Growth = 0;
nMdfFile.IsPrimaryFile = true;

fileGroup.Files.Add(nMdfFile);
nDB.FileGroups.Add(fileGroup);

nLogFile.Parent = nDB;
nLogFile.Name = newDataBaseName + "_Log";
nLogFile.FileName = dbPath + dbFileName + "_Log.ldf";
nDB.LogFiles.Add(nLogFile);
nDB.Create();
sqlsvr.ConnectionContext.Disconnect();
}

posted @ 2018-07-09 15:42  以函  阅读(138)  评论(0编辑  收藏  举报