Nilesh Barai
Nilesh Barai

Reputation: 1322

Referring external DLLs in WPF crashing Application

I have used following 3 SQL Server (Version SQL Server 2008 R2) DLLs in a WPF project:

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.

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. (These DLLs are required to find out the SQL Server DATA Root Directory.)

Following the code which I am trying to execute in my 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.

Upvotes: 0

Views: 219

Answers (1)

Praveen Paulose
Praveen Paulose

Reputation: 5771

When distributing certain packages, just including the dll within your package may not help. You need to read the distribution guidelines of those libraries.

In the case of SMO, this is what MSDN says

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.

If you have these support packages installed with your external libraries, your problem should be solved.

Upvotes: 1

Related Questions