使用SMO库从C#中的Application运行.sql文件

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

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

using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; 

在检查天气数据库是否存在后,我编写了运行脚本的代码。

当我运行代码时,弹出一个意外的exception。 我真的知道它是什么意思。 你可以帮助我摆脱这个例外。

我也附加了我的代码和exception预览。

请帮助我…我已经用Google搜索过多了。

在此处输入图像描述

 **Here are the Exception Details.** 

System.IO.FileLoadException未处理HResult = -2146232799 Message =混合模式程序集是针对运行时的版本’v2.0.50727’构建的,如果没有其他配置信息,则无法在4.0运行时加载。 Source = mscorlib StackTrace:System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly程序集,String name,Boolean throwOnError,Boolean ignoreCase,ObjectHandleOnStack类型),位于System.Reflection.RuntimeAssembly.GetType(String name,Boolean throwOnError,Boolean ignoreCase)。 Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query,ExecutionTypes executionType,Int32&statementsToReverse)中的Reflection.Assembly.GetType(String name,Boolean throwOnError)。 ExecutionTypes executionType)在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand)位于McFarlaneIndustriesPOSnamespace.SplashScreen.CreateDatabase()的e:\ Works \ McFarlane Industries \ McFarlane Industries销售点源代码\ McFarlaneIndustries \ SplashScreen.cs: McFarlaneIndustriesPOSnamespace.SplashScreen.splashScreenTimer_Tick(对象发送者,EventArgs e)第139行 )在e:\ Works \ McFarlane Industries \ McFarlane Industries销售点源代码\ McFarlaneIndustries \ SplashScreen.cs:在System.Windows.Forms.Timer.TimerNativeWindow的System.Windows.Forms.Timer.OnTick(EventArgs e)第159行在System.Windows.Forms的System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)的System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)中的.WndProc(Message&m)。 System.Windows.Forms上的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)中的Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)位于E:\ Works \ McFarlane Industries \ McFarlane Industries Point of S的McFarlaneIndustriesPOSnamespace.Program.Main()的System.Windows.Forms.Application.Run(Form mainForm)中的.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) ale源代码\ McFarlaneIndustries \ Program.cs:在System.Runtime.Hosting.ManifestRunner的System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly,String [] args)的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)第19行System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext,String [] activationCustomData)中的System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()处于System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)中的.Run(Boolean checkAptModel) )System.Activator.CreateInstance(ActivationContext activationContext),位于System.Threading.Thread上的System.Threading.Thread.ThreadStart_Context(对象状态)的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone(),执行文本执行上下文,执行文本执行上下文System.Threading.ExecutionContext.Run(ExecutionContext exec)中的对象状态,Boolean preserveSyncCtx) System.Threading.ThreadHelper.ThreadStart()中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态)的utionContext,ContextCallback回调,Object state,Boolean preserveSyncCtx):InnerException:

看起来你正在尝试加载.net 2版本的Microsoft.SqlServer.xxxx.dll。 更新您对.net4版本的引用(首选),或将以下口头禅添加到节点下的app.config中

     

允许加载.net 2程序集。 详情请见此处 。