C# 使用SQL Server管理对象(SMO)开发人员计算机32位-SQL Server 64位

C# 使用SQL Server管理对象(SMO)开发人员计算机32位-SQL Server 64位,c#,sql-server,clickonce,smo,C#,Sql Server,Clickonce,Smo,我在32位Win7机器上开发了一个Windows窗体应用程序,并在同一台机器上安装了一个SQL Server 2008进行测试。版本inviroment的数据库服务器具有64位。当我在与64位SQL Server连接的版本inviroment中的客户端(32位)上安装应用程序时,当我的代码尝试使用SQL Server管理对象(SMO)时,我遇到以下失败: 导致故障的代码: private static bool createDatabase(string dbName, stri

我在32位Win7机器上开发了一个Windows窗体应用程序,并在同一台机器上安装了一个SQL Server 2008进行测试。版本inviroment的数据库服务器具有64位。当我在与64位SQL Server连接的版本inviroment中的客户端(32位)上安装应用程序时,当我的代码尝试使用SQL Server管理对象(SMO)时,我遇到以下失败:

导致故障的代码:

        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;
    }
失败消息:

Anwendung:XingaAdmin.exe 框架版本:v4.0.30319 Beschreibung:Der Prozess wurde aufgrund einer unbehandlet Ausnahme beendet。 AusnahmeInformation:System.IO.FileNotFoundException 斯塔佩尔: bei System.Reflection.RuntimeAssembly.\u nLoad(System.Reflection.AssemblyName、System.String、System.Security.Policy.Evidence、System.Reflection.RuntimeAssembly、System.Threading.StackScrawlMark 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) bei System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(System.Reflection.AssemblyName,System.Security.Policy.Evidence,System.Threading.StackScrawlMark ByRef,Boolean,Boolean) bei System.Reflection.RuntimeAssembly.InternalLoad(System.String、System.Security.Policy.Evidence、System.Threading.StackScrawlMark ByRef、Boolean) bei系统.反射.组装.加载(系统.字符串) bei Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(System.String,Microsoft.SqlServer.Management.Common.ExecutionTypes,Int32 ByRef) beiMicrosoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(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()

数据库创建得很好,但不执行创建表的sql脚本

是否可能必须使用64位版本的SQL Server管理对象(SMO)?但是如果,我怎么才能得到这个版本。我无法在32位计算机上安装64位版本。

包含相同的堆栈跟踪,但堆栈转储还包含有关未能加载哪个SMO程序集的信息。使用try-catch块转储异常中的堆栈跟踪

SQL Server计算机需要安装64位SMO库。

包含相同的堆栈跟踪,但堆栈转储还包含有关未能加载哪个SMO程序集的信息。使用try-catch块转储异常中的堆栈跟踪


SQL Server计算机需要安装64位SMO库。

作为补充,如果CREATE DATABASE命令没有失败,当前方法将返回true,因为在将其转换为纯SQL并在数据库上运行时,它不会引发任何异常。如果CREATE DATABASE命令成功,但实际命令错误,您会怎么做?在该命令运行后,在返回true之前,您应该检查数据库是否存在。好的,但我在服务器上检查了数据库是否正确创建。那么,您作为参数传入的connStr看起来如何?数据源=SBS2008\SAGE;持久安全信息=True;用户ID=*****;密码=*******;初始目录=*******如果您的可执行文件在32位计算机上运行,它将使用32位版本的SMO,可以毫无问题地连接到远程64位服务器。我认为问题出在脚本内部,或者在创建数据库后重建的连接字符串中。您能展示一下这些部分吗?作为一条补充线,如果CREATE DATABASE命令没有失败,您当前的方法将返回true,因为当它转换为纯SQL并在数据库上运行时,它不会抛出任何异常。如果CREATE DATABASE命令成功,但实际命令错误,您会怎么做?在该命令运行后,在返回true之前,您应该检查数据库是否存在。好的,但我在服务器上检查了数据库是否正确创建。那么,您作为参数传入的connStr看起来如何?数据源=SBS2008\SAGE;持久安全信息=True;用户ID=*****;密码=*******;初始目录=*******如果您的可执行文件在32位计算机上运行,它将使用32位版本的SMO,可以毫无问题地连接到远程64位服务器。我认为问题出在脚本内部,或者在创建数据库后重建的连接字符串中。你能给我看看这些零件吗?