这是一个奇怪的问题。我们有一个程序可以在Microsoft SQL Server 2012中使用C#安装2个数据库。
卸载程序后,数据库不会被删除。当用户重新安装程序时,它会检查数据库是否仍在服务器内,然后删除它们,然后重新创建已删除的数据库。
数据库.bak
文件在C:/Program Files
或同等文件夹中创建。现在的问题是,如果你在卸载后手动删除.bak
文件并尝试重新安装程序,它就会失败(显然)。
但是如果你尝试三次安装程序,它最终会设法创建2个数据库。
这里是C#代码
String path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = System.IO.Path.GetDirectoryName(path);
Server server = InitializeServer(log);
server.ConnectionContext.Connect();
Database db = server.Databases["ADB"];
if (db != null)
{
try
{
db.Drop();//-------------------------First try fails here
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
try //----------------- second try database ADB gets created
{
Restore restore = new Restore();
// Set type of backup to be performed to database
restore.Database = "ADB";
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
// Create the Restore database ldf & mdf file name
String dataFileLocation = path + "ADB" + ".mdf";
String logFileLocation = path + "ADB" + "_Log.ldf";
RelocateFile rf = new RelocateFile("ADB", dataFileLocation);
// Set up the backup device to use filesystem.
restore.Devices.AddDevice(path + "\\ADB.bak", DeviceType.File);
System.Data.DataTable logicalRestoreFiles = restore.ReadFileList(server);
restore.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), dataFileLocation));
restore.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFileLocation));
restore.NoRecovery = false;
restore.SqlRestore(server);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
// Installing 2nd database----------------------------------------------
server.ConnectionContext.Connect();
Database db1 = server.Databases["BDB"];
if (db1 != null)
{
try
{
db1.Drop();//-------------------------Second try fails here
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
try //----------------- Third try database BDB gets created
{
Restore restore2 = new Restore();
restore2.Database = "BDB";
restore2.Action = RestoreActionType.Database;
restore2.ReplaceDatabase = true;
String dataFileLocation = path + "BDB" + ".mdf";
String logFileLocation = path + "BDB" + "_Log.ldf";
RelocateFile rf = new RelocateFile("BDB", dataFileLocation);
restore2.Devices.AddDevice(path + "\\BDB.bak", DeviceType.File);
System.Data.DataTable logicalRestoreFiles = restore2.ReadFileList(server);
restore2.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), dataFileLocation));
restore2.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFileLocation));
restore2.NoRecovery = false;
restore2.SqlRestore(server);
}
我的问题是,有没有办法在手动删除.bak
文件后分离服务器数据库?为什么数据库是在第二次尝试时创建的?
编辑: 这是删除数据库时收到的错误消息
Microsoft.SqlServer.Management.Smo.FailedOperationException: Drop failed for Database 'ADB'. ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. ---> System.Data.SqlClient.SqlException: Cannot drop the database 'ADB', because it does not exist or you do not have permission.
at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ExecuteNonQuery(StringCollection queries, Boolean includeDbContext)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.DropImplWorker(Urn& urn)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.DropImpl()
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.DropImpl()
at SetupScripts.SetupDataBase.Execute(FrmLog log)