Run .sql file from Application in C# using SMO library

我的未来我决定 提交于 2019-12-05 00:32:04

问题


I am building an application in C# visual studio 2012 RC.

I have to run the scripts within the application. for this i am using. These two libraries. My application's target .net framework is 4.5.

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

I wrote the code to run the scripts after checking weather database exists or not.

When i run the code an unexpected exception pops up. i really know what does it mean. Could you please help me in order to get rid of this exception.

I am attaching the my code and exception preview also.

Please HELP ME... I have googled too much already.

**Here are the Exception Details.**

System.IO.FileLoadException was unhandled HResult=-2146232799 Message=Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. Source=mscorlib StackTrace: at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type) at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase) at System.Reflection.Assembly.GetType(String name, Boolean throwOnError) at Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executionType, Int32& statementsToReverse) at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType) at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand) at McFarlaneIndustriesPOSnamespace.SplashScreen.CreateDatabase() in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\SplashScreen.cs:line 139 at McFarlaneIndustriesPOSnamespace.SplashScreen.splashScreenTimer_Tick(Object sender, EventArgs e) in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\SplashScreen.cs:line 159 at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at McFarlaneIndustriesPOSnamespace.Program.Main() in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) at System.Activator.CreateInstance(ActivationContext activationContext) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:


回答1:


It looks like you're trying to load .net 2 version of Microsoft.SqlServer.xxxx.dll. Update your references to a .net4 version (preferable), or add the following mantra to your app.config under the <configuration> node

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

to allow loading of .net 2 assemblies. See here for details.



来源:https://stackoverflow.com/questions/11286000/run-sql-file-from-application-in-c-sharp-using-smo-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!