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

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

I need to run scripts in an application. for this I use. These are two libraries. The .net platform for my application is 4.5.

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

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

When I run the code, an unexpected exception pops up. I really know what that means. Could you help me to get rid of this exception.

I also add my code and preview exceptions.

HELP ME ... I've already searched too much.

enter image description here

**Here are the Exception Details.**

System.IO.FileLoadException HResult = -2146232799 = "v2.0.50727" 4.0 . = mscorlib :       System.Reflection.RuntimeAssembly.GetType( RuntimeAssembly, , throwOnError, ignoreCase, ObjectHandleOnStack)       System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)       System.Reflection.Assembly.GetType( , Boolean throwOnError)       Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executeType, Int32 & statementsToReverse)       Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executeType)       Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery( sqlCommand)       McFarlaneIndustriesPOSnamespace.SplashScreen.CreateDatabase() e:\Works\McFarlane Industries\McFarlane Industries Point of Sale \McFarlaneIndustries\SplashScreen.cs: 139       McFarlaneIndustriesPOSnamespace.SplashScreen.splashScreenTimer_Tick ( , EventArgs e) e:\Works\McFarlane Industries\McFarlane Industries Point of Sale \McFarlaneIndustries\SplashScreen.cs: 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.UnsafeNativeMethods.DispatchMessageW(MSG & msg)       System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32, 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)       McFarlaneIndustriesPOSnamespace.Program.Main() in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale \McFarlaneIndustries\Program.cs: 19       System.AppDomain._nExecuteAssembly ( RuntimeAssembly, String [] args)       System.AppDomain.nExecuteAssembly( RuntimeAssembly, String [] args)       System.Runtime.Hosting.ManifestRunner.Run(Boolean 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_Context ( )       System.Threading.ExecutionContext.RunInternal(ExecutionContext executeContext, ContextCallback, , Boolean preserveSyncCtx)       System.Threading.ExecutionContext.Run(ExecutionContext executeContext, ContextCallback, , Boolean preserveSyncCtx)       System.Threading.ExecutionContext.Run(ExecutionContext executeContext, ContextCallback, )       System.Threading.ThreadHelper.ThreadStart() InnerException:

+5
1

, .net 2 Microsoft.SqlServer.xxxx.dll. .net4 () app.config <configuration> node

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

.net 2. . .

+5

All Articles