简体   繁体   English

在WPF崩溃的应用程序中引用外部DLL

[英]Referring external DLLs in WPF crashing Application

I have used following 3 SQL Server (Version SQL Server 2008 R2) DLLs in a WPF project: 我在WPF项目中使用了以下3个SQL Server(版本SQL Server 2008 R2)DLL:

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo

Project works fine on the machine on which it was developed. 项目在其开发的机器上运行良好。 However if I try to move the .exe to some other machine then application gets crashed. 但是,如果我尝试将.exe移至其他计算机,则应用程序将崩溃。

I have set Copy Local = True for all the 3 DLLs so that during compilation it should copy all the 3 DLLs inside Debug folder. 我已经为所有3个DLL设置了Copy Local = True ,因此在编译期间它应该复制Debug文件夹中的所有3个DLL。 (These DLLs are required to find out the SQL Server DATA Root Directory.) (这些DLL是查找SQL Server DATA根目录所必需的。)

Following the code which I am trying to execute in my App. 遵循我尝试在我的App中执行的代码。

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);

Not able to find out why my application is crashing. 无法找出我的应用程序崩溃的原因。

When distributing certain packages, just including the dll within your package may not help. 分发某些程序包时,仅在程序包中包含dll可能无济于事。 You need to read the distribution guidelines of those libraries. 您需要阅读这些库的分发准则。

In the case of SMO, this is what MSDN says 对于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. 如果开发使用SQL Server管理对象的应用程序,则需要确保该应用程序的计算机上存在必需的支持文件。 The SQL Server 2008 feature pack contains a redistributable package for the SQL Server Management Objects. SQL Server 2008功能包包含用于SQL Server管理对象的可再发行程序包。

If you have these support packages installed with your external libraries, your problem should be solved. 如果您的外部库中安装了这些支持包,则应解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM