以编程方式创建数据库
-
29-09-2019 - |
题
我正在尝试通过C#编程创建数据库。我有用于数据库创建的脚本,当我从SQL Server Management Studio运行它们时可以正常工作。但是,当我从C#应用程序运行相同的脚本时,会发生以下错误:
类型“ Microsoft.sqlserver.management.common.common.executionfailureexception”类型的例外情况发生在Microsoft.sqlserver.connectioninfo.dll中。
关于为什么会发生这种情况有什么想法吗?
没有正确的解决方案
其他提示
只是为了让您知道,AC#框架工作将为您完成所有这些工作。http://www.sharparchitecture.net/ 它将从OO模型构建您的数据库和数据访问层。
公共字符串CreateTabase(字符串ipaddress,String用户名,字符串密码,字符串DB_FilePath){
Microsoft.SqlServer.Management.Smo.Server addDBserver = new Microsoft.SqlServer.Management.Smo.Server(ipAddress);
addDBserver.ConnectionContext.LoginSecure = false;
addDBserver.ConnectionContext.Login = UserName;
addDBserver.ConnectionContext.Password = Password;
try
{
//*Crerate Databse*
addDBserver.ConnectionContext.Connect();
FileInfo filedb = new FileInfo(DB_filepath);
string scriptdb = filedb.OpenText().ReadToEnd();
string scriptdb1 = scriptdb.Replace("GO", Environment.NewLine);
string scriptdb2 = scriptdb1.Replace("\r\nGO\r\n", "");
addDBserver.ConnectionContext.ExecuteNonQuery(scriptdb2);
addDBserver.ConnectionContext.Disconnect();
string Msg;
Msg = "db created successfully";
return Msg;
return true;
}
catch (Exception ex)
{
string Msg1 = "db notcreated successfully";
return ex.Message;
throw;
}
}
//Database created Successfully
不隶属于 StackOverflow