【发布时间】:2014-11-17 17:40:48
【问题描述】:
我正在 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 程序集,字符串名称,布尔 throwOnError,布尔忽略大小写, ObjectHandleOnStack 类型)在 System.Reflection.RuntimeAssembly.GetType(字符串名称,布尔值 throwOnError,布尔忽略大小写)在 System.Reflection.Assembly.GetType(字符串名称,布尔 throwOnError) 在 Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(字符串 查询,ExecutionTypes executionType,Int32& statementsToReverse) 在 Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(字符串 sqlCommand, ExecutionTypes executionType) 在 Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(字符串 sqlCommand) 在 test.main.btnLoadMasterData_Click(Object sender, C:\Users\marky\Documents\Visual Studio 中的 EventArgs e) 2013\Projects\Hours Analysis\Hours Analysis\Form1.vb:line 212 at System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,Int32 点击)在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精)
在 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(ApplicationContext 上下文)在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串 [] commandLine) 在 test.My.MyApplication.Main(String[] Args) 中 17d14f5c-a337-4978-8281-53493378c1071.vb:第 81 行 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 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) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 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(字符串连接字符串, SqlCredential 凭证)在 System.Data.SqlClient.SqlConnection..ctor(字符串连接字符串)
在 test.main.btnLoadMasterData_Click(Object sender, EventArgs e) 中 C:\Users\marky\Documents\Visual Studio 2013\Projects\Hours Analysis\Hours Analysis\Form1.vb:line 210 at System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,Int32 点击)在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精)
在 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(ApplicationContext 上下文)在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串 [] commandLine) 在 test.My.MyApplication.Main(String[] Args) 中 17d14f5c-a337-4978-8281-53493378c1071.vb:第 81 行 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 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) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()
【问题讨论】:
-
我不认为这具体是 SQL Server 问题,也不是您的 .mdf 问题,而是某种 .Net Framework 问题。你能从异常中发布整个堆栈跟踪吗?
-
我怀疑您使用的是过时的 SMO 程序集,或者是在不同的 .Net Framework 版本下构建的用户类库程序集。
标签: vb.net winforms visual-studio-2013 sql-server-2012 smo