我正嘗試以編程方式在C#中創建數據庫。我有用於數據庫創建的腳本,當我從SQL Server Management Studio運行它們時它們工作正常。然而,當我運行從我的C#應用程序相同的腳本,會出現以下錯誤:以編程方式創建數據庫
型「Microsoft.SqlServer.Management.Common.ExecutionFailureException」未處理的異常發生在Microsoft.SqlServer.ConnectionInfo.dll
任何想法爲什麼這可能發生?
我正嘗試以編程方式在C#中創建數據庫。我有用於數據庫創建的腳本,當我從SQL Server Management Studio運行它們時它們工作正常。然而,當我運行從我的C#應用程序相同的腳本,會出現以下錯誤:以編程方式創建數據庫
型「Microsoft.SqlServer.Management.Common.ExecutionFailureException」未處理的異常發生在Microsoft.SqlServer.ConnectionInfo.dll
任何想法爲什麼這可能發生?
只是爲了讓你知道有一個C#框架的工作,將爲你做這一切。 http://www.sharparchitecture.net/它將從OO模型構建您的數據庫和數據訪問層。
公共字符串的CreateDatabase(字符串ip地址,用戶名字符串,字符串密碼,串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
發佈您的代碼,請。 – Radu 2010-08-19 11:42:30
請發佈您的SQL – TheAlbear 2010-08-19 11:46:55