我在VS2013中构建一个VB.NET桌面应用程序,它使用在SQL Server 2012中创建的.mdf文件。我将.mdf文件导入到应用程序中。除了用于查看/修改数据库表的datagridviews,我需要在用户单击按钮时执行SQL脚本。
我一直在尝试几个我没有找到运气的例子。我在ExecuteNonQuery行上收到了FileLoadException错误:
未处理的类型' System.IO.FileLoadException'发生了 在mscorlib.dll中
其他信息:混合模式程序集是针对版本构建的 ' V2.0.50727'运行时的运行时无法在4.0运行时加载 没有其他配置信息。
"故障排除提示"部分已经"确保该文件是有效的.NET Framework程序集"。我点击了那个链接,但那些信息对我来说并不是很有帮助,因为我真的不明白它的含义。
到目前为止,我已尝试过这两个例子:
Dim sqlScript As String = "C:\HL_Time_Entry_Reporting\SQL Scripts\stage_employee_hours_load.sql"
Dim connString As String = "Server=(LocalDB)\v11.0;Integrated Security=True;AttachDbFilename=C:\HL_Time_Entry_Reporting\Database\hours_analysis.mdf"
Dim connectionString As String = connString,
scriptText As String = sqlScript
Dim sqlConnection As New SqlConnection(connectionString)
Dim svrConnection As New ServerConnection(sqlConnection)
Dim server As New Server(svrConnection)
server.ConnectionContext.ExecuteNonQuery(scriptText)
来自this site的。和
Dim sqlConnectionString As String = "Server=(LocalDB)\v11.0;Integrated Security=True;AttachDbFilename=C:\HL_Time_Entry_Reporting\Database\hours_analysis.mdf"
Dim file As New FileInfo("C:\HL_Time_Entry_Reporting\SQL Scripts\stage_employee_hours_load.sql")
Dim script As String = file.OpenText().ReadToEnd()
Dim conn As New SqlConnection(sqlConnectionString)
Dim server As New Server(New ServerConnection(conn))
server.ConnectionContext.ExecuteNonQuery(script)
来自this site。
有一件事是,我不相信我有正确的连接字符串。那是我对datagridviews的连接字符串,它工作正常,但是我也应该用它来连接sql脚本文件吗?
更新
每个请求,这里是堆栈跟踪:
在System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly,String name,Boolean throwOnError,Boolean ignoreCase, ObjectHandleOnStack类型)at System.Reflection.RuntimeAssembly.GetType(String name,Boolean throwOnError,Boolean ignoreCase)at System.Reflection.Assembly.GetType(String name,Boolean throwOnError) 在 Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(字符串 query,ExecutionTypes executionType,Int32& statementsToReverse)at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(字符串 sqlCommand,ExecutionTypes executionType)at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(字符串 sqlCommand)at test.main.btnLoadMasterData_Click(Object sender, EventArgs e)在C:\ Users \ marky \ Documents \ Visual Studio中 2013 \ Projects \ Hours Analysis \ Hours Analysis \ Form1.vb:212行at System.Windows.Forms.Control.OnClick(EventArgs e)at System.Windows.Forms.Button.OnClick(EventArgs e)at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)at at System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons 按钮,Int32点击)at System.Windows.Forms.Control.WndProc(Message& m)at System.Windows.Forms.ButtonBase.WndProc(Message& m)at System.Windows.Forms.Button.WndProc(Message& m)at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr) hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 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(ApplicationContext context)at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[] commandLine)at test.My.MyApplication.Main(String [] Args)in 17d14f5c-a337-4978-8281-53493378c1071.vb:第81行 System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)在System.AppDomain.ExecuteAssembly(String assemblyFile, 证据assemblySecurity,String [] args)at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ThreadHelper.ThreadStart()
更新#2
正如建议的那样,我在this SO question中将启动标记添加到我的appconfig文件中,但在该行上出现了新错误" Dim server As New Server(New ServerConnection(conn))"在第二个代码示例中。它是TypeInitializationException。这是信息:
未处理的类型' System.TypeInitializationException' 发生在System.Data.dll
中附加信息:类型初始化程序 ' System.Data.SqlClient.SqlConnection'抛出异常。
堆栈追踪:
在System.Data.SqlClient.SqlConnection..ctor()处 System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential凭证) System.Data.SqlClient.SqlConnection..ctor(String connectionString)
在test.main.btnLoadMasterData_Click(Object sender,EventArgs e)中 C:\ Users \ marky \ Documents \ Visual Studio 2013 \ Projects \ Hours 分析\小时分析\ Form1.vb:第210行 System.Windows.Forms.Control.OnClick(EventArgs e)at System.Windows.Forms.Button.OnClick(EventArgs e)at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)at at System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons 按钮,Int32点击)at System.Windows.Forms.Control.WndProc(Message& m)at System.Windows.Forms.ButtonBase.WndProc(Message& m)at System.Windows.Forms.Button.WndProc(Message& m)at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr) hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 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(ApplicationContext context)at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[] commandLine)at test.My.MyApplication.Main(String [] Args)in 17d14f5c-a337-4978-8281-53493378c1071.vb:第81行 System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)在System.AppDomain.ExecuteAssembly(String assemblyFile, 证据assemblySecurity,String [] args)at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:2)
您可能正在使用过时版本的SMO库,如下所述:https://stackoverflow.com/a/11286124/109122。
如果链接推荐的配置设置不起作用,请尝试使用这个更简单的设置:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
不幸的是,配置文件更改似乎并未修复所有情况。