簡體   English   中英

在WPF崩潰的應用程序中引用外部DLL

[英]Referring external DLLs in WPF crashing Application

我在WPF項目中使用了以下3個SQL Server(版本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 Server DATA根目錄所必需的。)

遵循我嘗試在我的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);

無法找出我的應用程序崩潰的原因。

分發某些程序包時,僅在程序包中包含dll可能無濟於事。 您需要閱讀這些庫的分發准則。

對於SMO,這就是MSDN所說的

如果開發使用SQL Server管理對象的應用程序,則需要確保該應用程序的計算機上存在必需的支持文件。 SQL Server 2008功能包包含用於SQL Server管理對象的可再發行程序包。

如果您的外部庫中安裝了這些支持包,則應解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM