1

I develop a Windows Forms Application on a 32 bit Win7 machine and have a SQL Server 2008 on the same machine for testing. The database server of the release inviroment has 64 bit. When I install the application on a client (32 bit) in the release inviroment which is connected with the 64 bit SQL Server I get the following failure when my code try to use SQL Server Management Objects (SMO):

Code which causes the failure:

        private static bool createDatabase(string dbName, string sqlPath, string connStr)
    {
        FileInfo file = new FileInfo(sqlPath + dbName + ".sql");
        string strscript = file.OpenText().ReadToEnd();
        bool result;
        string test = "CREATE DATABASE [" + dbName + "]";

        try
        {
            SqlConnection connection = new SqlConnection(connStr);
            using (connection)
            {
                using (SqlCommand sqlCmd = new SqlCommand(test, connection))
                {
                    connection.Open();
                    sqlCmd.ExecuteNonQuery();
                    connection.Close();
                    result = true;
                }
            }

            if (result == true)
            {
                connStr = connStr + ";Initial Catalog=" + dbName;
                SqlConnection sqlConnection = new SqlConnection(connStr);
                ServerConnection svrConnection = new ServerConnection(sqlConnection);
                Server server = new Server(svrConnection);

                server.ConnectionContext.ExecuteNonQuery(strscript); // Here the app crashes
            }
        }
        catch (SqlException ae)
        {
            result = false;
            System.Windows.Forms.MessageBox.Show(ae.Message.ToString());
        }
        return result;
    }

Failure message:

Anwendung: XingaAdmin.exe Frameworkversion: v4.0.30319 Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet. Ausnahmeinformationen: System.IO.FileNotFoundException Stapel: bei System.Reflection.RuntimeAssembly._nLoad(System.Reflection.AssemblyName, System.String, System.Security.Policy.Evidence, System.Reflection.RuntimeAssembly, System.Threading.StackCrawlMark ByRef, Boolean, Boolean, Boolean) bei System.Reflection.RuntimeAssembly.nLoad(System.Reflection.AssemblyName, System.String, System.Security.Policy.Evidence, System.Reflection.RuntimeAssembly, System.Threading.StackCrawlMark ByRef, Boolean, Boolean, Boolean) bei System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(System.Reflection.AssemblyName, System.Security.Policy.Evidence, System.Threading.StackCrawlMark ByRef, Boolean, Boolean) bei System.Reflection.RuntimeAssembly.InternalLoad(System.String, System.Security.Policy.Evidence, System.Threading.StackCrawlMark ByRef, Boolean) bei System.Reflection.Assembly.Load(System.String) bei Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(System.String, Microsoft.SqlServer.Management.Common.ExecutionTypes, Int32 ByRef) bei Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(System.String, Microsoft.SqlServer.Management.Common.ExecutionTypes) bei XingaCommonClasses.Utilities.Database.DatabaseHelper.createDatabase(System.String, System.String, System.String) bei XingaCommonClasses.Utilities.Database.DatabaseHelper.checkDatabase(System.Collections.Generic.List`1, System.String) bei XingaAdmin.Program.Main()

The database is created fine, but the sql-script with the table creation is not executed.

Is it possible that I have to use the 64 bit version of the SQL Server Management Objects (SMO)? But if, how can I get this version. I cannot install the 64 bit version on a 32 bit machine.

23
  • Just as a side line, your current method will return true if the CREATE DATABASE command does not fail, as in it doesn't throw any exceptions when it is translated into pure SQL and run on the database. What do you do if the CREATE DATABASE command succeeds but the actual command is wrong? You should check for the existence of the database after that command is run, BEFORE returning true. Commented Jun 6, 2012 at 9:20
  • Ok, but I checked on the server that the database was properly created. Commented Jun 6, 2012 at 9:25
  • So how does your connStr look like, the one you're passing in as a parameter? Commented Jun 6, 2012 at 9:28
  • Data Source=SBS2008\SAGE;Persist Security Info=True;User ID=******;Password=*******;Initial Catalog=******** Commented Jun 6, 2012 at 9:35
  • If your executable runs on a 32bit machine, it will use the 32bit version of SMO that could connect to a remote 64bit server without problems. I think the problem is inside your script or in the connection string rebuilt after the create database. Could you show something of these parts? Commented Jun 6, 2012 at 9:43

1 Answer 1

1

This question contains the same stack trace, but the stack dump also contains information about which SMO assembly failed to load. Use a try-catch block to dump the stack trace in the exception.

The SQL Server machine needs to have the 64bit SMO library installed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.