使用SMO库从C#中的应用程序运行.sql文件

使用SMO库从C#中的应用程序运行.sql文件,c#,.net,sql,sql-server-2008,smo,C#,.net,Sql,Sql Server 2008,Smo,我正在C#visual studio 2012 RC中构建一个应用程序 我必须在应用程序中运行脚本。为此,我正在使用。 这两个图书馆。 我的应用程序的目标.net框架是4.5 using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; 在检查天气数据库是否存在后,我编写了运行脚本的代码 当我运行代码时,会弹出一个意外的异常。我真的知道这是什么意思。你能帮我摆脱这个例外吗 我还附加了

我正在C#visual studio 2012 RC中构建一个应用程序

我必须在应用程序中运行脚本。为此,我正在使用。 这两个图书馆。 我的应用程序的目标.net框架是4.5

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
在检查天气数据库是否存在后,我编写了运行脚本的代码

当我运行代码时,会弹出一个意外的异常。我真的知道这是什么意思。你能帮我摆脱这个例外吗

我还附加了我的代码和异常预览

请帮帮我。。。我已经在谷歌上搜索太多了

**Here are the Exception Details.**

System.IO.FileLoadException未处理 HResult=-2146232799 Message=混合模式程序集是根据运行时版本“v2.0.50727”生成的,如果没有其他配置信息,则无法在4.0运行时中加载。 Source=mscorlib 堆栈跟踪: 在System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly程序集、字符串名称、布尔throwOnError、布尔ignoreCase、ObjectHandleOnStack类型) 在System.Reflection.RuntimeAssembly.GetType(字符串名称、布尔throwOnError、布尔ignoreCase) 位于System.Reflection.Assembly.GetType(字符串名称,布尔错误) 位于Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(字符串查询、ExecutionTypes executionType、Int32和statementsToReverse) 位于Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(字符串sqlCommand,ExecutionTypes executionType) 位于Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(字符串sqlCommand) 在e:\Works\McFarlane Industries\McFarlane Industries销售点源代码\McFarlaneIndustries\SplashScreen.cs:第139行中的McFarlaneIndustriesPOSnamespace.SplashScreen.CreateDatabase()处 在e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\SplashScreen.cs中的McFarlaneIndustries PostNamespace.SplashScreen.SplashScreen Timer_勾选(对象发送方,事件参数e):第159行 在System.Windows.Forms.Timer.OnTick(EventArgs e)中 在System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&m)中 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam) 在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中 位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData) 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文) 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文) 在System.Windows.Forms.Application.Run(Form mainForm)中 位于e:\Works\McFarlane Industries\McFarlane Industries销售点源代码\McFarlaneIndustries\Program.cs中的McFarlaneIndustriesPOSnamespace.Program.Main():第19行 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args) 位于System.AppDomain.nExecuteAssembly(运行时程序集,字符串[]args) at System.Runtime.Hosting.ManifestRunner.Run(布尔checkAptModel) 在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()上 在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext ActivationContext,String[]activationCustomData)中 位于System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext ActivationContext) 位于System.Activator.CreateInstance(ActivationContext ActivationContext) 位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()处 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处
InnerException:

看起来您正在尝试加载.net 2版本的Microsoft.SqlServer.xxxx.dll。更新对.net4版本的引用(最好),或者在
节点下的app.config中添加以下咒语

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  <requiredRuntime version="v4.0" />
</startup>


以允许加载.net 2程序集。请参阅。

请发布异常详细信息。我已编辑了问题,并根据您的要求添加了异常详细信息。快照显示异常弹出的点。请查看详细信息和快照。我的程序集中没有我的项目中引用的这两个库。然后下载SQLServer2008R2的MSI文件“SharedManagementObjects.MSI”x64。之后,会弹出此异常。现在我将从SQLServer2012网页下载此文件。我希望现在.net冲突不会发生。我走对了吗,先生。我真的安装了它吗。或者别的什么。这是正确的方法吗?请告诉我们sqlsvr2012是否有.net 4 SMO API。如果没有,请使用app.config修改使事情正常运行。你太棒了。。。你是最棒的。。。你太棒了。。。先生我现在是你的粉丝。。。我的问题解决了。。。谢谢很多。很多很多很多…Solubion:我已经安装了Microsoft管理对象sql Server 2012表单官方网站。并将其安装在我的电脑上。然后我将该代码放在我的app.config中。文件现在它的工作和数据库已成功安装。。。感谢斯宾德。。。真的感谢他。。我在这个错误上花了很多时间。