在 WPF 崩溃应用程序中引用外部 DLL
Referring external DLLs in WPF crashing Application
我在 WPF 项目中使用了以下 3 SQL 服务器(版本 SQL Server 2008 R2)DLL:
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo
项目在开发它的机器上运行良好。但是,如果我尝试将 .exe
移动到其他机器,则应用程序会崩溃。
我已经为所有 3 个 DLL 设置了 Copy Local = True
,以便在编译期间它应该复制 Debug 文件夹中的所有 3 个 DLL。 (需要这些 DLL 才能找到 SQL 服务器数据根目录。)
按照我试图在我的应用程序中执行的代码。
try
{
MessageBox.Show("trying to open connection");
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();
con = new SqlConnection(conStr);
con.Open();
MessageBox.Show("connection opened ");
var serverConnection = new ServerConnection(con);
var server = new Server(serverConnection);
var defaultDataPath = string.IsNullOrEmpty(server.Settings.DefaultFile) ? server.MasterDBPath : server.Settings.DefaultFile;
var defaultLogPath = string.IsNullOrEmpty(server.Settings.DefaultLog) ? server.MasterDBLogPath : server.Settings.DefaultLog;
string restoreSql = @"RESTORE DATABASE [MyDB]
FROM DISK = '" + this.FilePath + @"'
WITH
MOVE 'MyDB' TO '" + defaultDataPath + @"\MyDB.mdf',
MOVE 'MyDB_Log' TO '" + defaultLogPath + @"\MyDB_Log.ldf',
RECOVERY, REPLACE, STATS = 10";
SqlCommand restoreCmd = new SqlCommand(restoreSql, con);
int status = restoreCmd.ExecuteNonQuery();
completedFlag = true;
}
catch (Exception ex)
{
MessageBox.Show("Database Restore Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
if (completedFlag)
MessageBox.Show("Database restore successful.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show("SYSTEM ERROR: Failed restoring database.\nPlease restart SQL Server Service and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
无法找出我的应用程序崩溃的原因。
分发某些包时,仅在包中包含 dll 可能无济于事。您需要阅读这些库的分发指南。
对于 SMO,MSDN 是这样说的
If you develop an application that uses SQL Server Management Objects,
you need to make sure that the necessary support files are present on
the computer with the application. The SQL Server 2008 feature pack
contains a redistributable package for the SQL Server Management
Objects.
如果您在外部库中安装了这些支持包,您的问题应该可以解决。
我在 WPF 项目中使用了以下 3 SQL 服务器(版本 SQL Server 2008 R2)DLL:
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo
项目在开发它的机器上运行良好。但是,如果我尝试将 .exe
移动到其他机器,则应用程序会崩溃。
我已经为所有 3 个 DLL 设置了 Copy Local = True
,以便在编译期间它应该复制 Debug 文件夹中的所有 3 个 DLL。 (需要这些 DLL 才能找到 SQL 服务器数据根目录。)
按照我试图在我的应用程序中执行的代码。
try
{
MessageBox.Show("trying to open connection");
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();
con = new SqlConnection(conStr);
con.Open();
MessageBox.Show("connection opened ");
var serverConnection = new ServerConnection(con);
var server = new Server(serverConnection);
var defaultDataPath = string.IsNullOrEmpty(server.Settings.DefaultFile) ? server.MasterDBPath : server.Settings.DefaultFile;
var defaultLogPath = string.IsNullOrEmpty(server.Settings.DefaultLog) ? server.MasterDBLogPath : server.Settings.DefaultLog;
string restoreSql = @"RESTORE DATABASE [MyDB]
FROM DISK = '" + this.FilePath + @"'
WITH
MOVE 'MyDB' TO '" + defaultDataPath + @"\MyDB.mdf',
MOVE 'MyDB_Log' TO '" + defaultLogPath + @"\MyDB_Log.ldf',
RECOVERY, REPLACE, STATS = 10";
SqlCommand restoreCmd = new SqlCommand(restoreSql, con);
int status = restoreCmd.ExecuteNonQuery();
completedFlag = true;
}
catch (Exception ex)
{
MessageBox.Show("Database Restore Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
if (completedFlag)
MessageBox.Show("Database restore successful.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show("SYSTEM ERROR: Failed restoring database.\nPlease restart SQL Server Service and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
无法找出我的应用程序崩溃的原因。
分发某些包时,仅在包中包含 dll 可能无济于事。您需要阅读这些库的分发指南。
对于 SMO,MSDN 是这样说的
If you develop an application that uses SQL Server Management Objects, you need to make sure that the necessary support files are present on the computer with the application. The SQL Server 2008 feature pack contains a redistributable package for the SQL Server Management Objects.
如果您在外部库中安装了这些支持包,您的问题应该可以解决。