【问题标题】:Deploying Linq-to-SQL C# application that contains service based database部署包含基于服务的数据库的 Linq-to-SQL C# 应用程序
【发布时间】:2016-08-06 06:31:13
【问题描述】:

我在 Visual Studio 中使用 Linq-to-SQL 构建了一个 C# 应用程序,它包含一个基于服务的数据库(.mdf 文件)。我想将它部署到另一台计算机上,以便在那里使用。

我已经在我的解决方案中使用 Visual Studio 安装程序 -> 安装项目创建了安装程序。我尝试将它安装在另一台计算机上。安装完成后,我可以打开应用程序了。

但是当我想访问数据库时,我得到了这个错误:

Microsoft .NET Framework:您的应用程序中发生了未处理的异常。如果单击继续,应用程序将忽略此错误并尝试继续。如果您点击退出,应用程序将立即关闭。

底层提供程序在打开时失败。

有人可以帮忙吗?我是 C# 编程新手,我使用 Microsoft SQL Server 2012 Express 和 Visual Studio 2015。谢谢

这是错误日志:

`有关调用即时 (JIT) 调试而不是此对话框的详细信息,请参阅此消息的末尾。

************** 异常文本 ************** System.Data.Entity.Core.EntityException:基础提供程序在打开时失败。 ---> System.Data.SqlClient.SqlException:尝试为文件 C:\Program Files\Techno zone\LocalACC\Database1.mdf 附加自动命名数据库失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 标识,SqlConnectionString connectionOptions,SqlCredential 凭据,对象 providerInfo,字符串 newPassword,SecureString newSecurePassword,布尔重定向用户实例,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPool 池,字符串 accessToken,布尔 applyTransientFaultHandling) System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool 池,DbConnection owningObject,DbConnectionOptions 选项,DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) 在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbCon nectionInternal oldConnection) 在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) 在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&连接)在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 重试) 在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget 目标t,Action2 operation, TInterceptionContext interceptionContext, Action3 正在执行,Action3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 操作)在 System.Data.Entity.Core.EntityClient.EntityConnection.Open() --- 内部异常堆栈跟踪结束 --- 在 System.Data.Entity .Core.EntityClient.EntityConnection.Open() 在 System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions) 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery 1.c__DisplayClass7.b__5() 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1..GetEnumerator>b__0() 在 System.Data。 Entity.Internal.LazyEnumerator1.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 SmallProject.AllTransactionForm.btnShowAll_Click(Object sender, EventArgs e) 在系统。 Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Con trol.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button。 WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** 加载的程序集 ************** mscorlib 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll -------------------------------------- 小项目 程序集版本:1.0.0.0 Win32版本:1.0.0.0 代码库:file:///C:/Program%20Files/Techno%20zone/LocalACC/SmallProject.exe ---------------------------------------------------- System.Windows.Forms 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll - - - - - - - - - - - - - - - - - - - - 系统 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------------------- System.Drawing 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll - - - - - - - - - - - - - - - - - - - - 系统配置 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------------------- System.Core 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll ---------------------------------------------------- System.Xml 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll -------------------------------------- 实体框架 程序集版本:6.0.0.0 Win32版本:6.1.40302.0 代码库:file:///C:/Program%20Files/Techno%20zone/LocalACC/EntityFramework.DLL ---------------------------------------------------- System.Data 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll -------------------------------------- EntityFramework.SqlServer 程序集版本:6.0.0.0 Win32版本:6.1.40302.0 代码库:file:///C:/Program%20Files/Techno%20zone/LocalACC/EntityFramework.SqlServer.DLL ---------------------------------------- 匿名托管的 DynamicMethods 程序集 程序集版本:0.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll ---------------------------------------------------- System.Numerics 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll ---------------------------------------------------- System.Data.OracleClient 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.Data.OracleClient/v4.0_4.0.0.0__b77a5c561934e089/System.Data.OracleClient.dll ---------------------------------------------------- System.Transactions 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll ---------------------------------------------------- System.Xml.Linq 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll ---------------------------------------------------- System.EnterpriseServices 程序集版本:4.0.0.0 Win32 版本:4.6.1532.0 构建者:NETFXREL1LAST

代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll

************** JIT 调试 ************** 要启用即时 (JIT) 调试,.config 文件用于此应用程序或计算机 (machine.config) 必须在 system.windows.forms 部分中设置 jitDebugging 值。还必须在启用调试的情况下编译应用程序。

例如:

启用 JIT 调试后,任何未处理的异常都将发送到计算机上注册的 JIT 调试器,而不是由该对话框处理。`

【问题讨论】:

  • 请发布错误(异常)详细信息,以便我们提供帮助
  • 您不能只将 mdf 文件复制到另一台计算机。另一台机器也应该安装了 Sql Server(至少 localdb)并且连接字符串应该是正确的。
  • 我在我的 PC 上尝试过(安装了 Sql Server),但仍然出现此错误。我不复制 mdf 文件,而是为我的应用程序创建一个安装项目。但是当我创建安装项目并在我的PC上安装它时,文件夹中没有数据库文件,只有EntityFramework.dll、EntityFramework.SqlServer.dll、myproject.exe和myrpoject.exe.config。
  • 这是我的连接字符串:Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\\Documents\Visual Studio 2015\Projects\SmallProject\SmallProject\Database1.mdf" ;集成安全=真
  • 安装应用程序基本上是复制文件。如果你只是将一个 mdf 文件放到某个目录中,就无法像那样访问它。

标签: c# .net entity-framework linq-to-sql sql-server-2012-express


【解决方案1】:

您是否尝试过分离然后附加数据库?
如果没有,请阅读下面简要介绍的解决方案。

如果我没记错的话,您使用的是 Sql Server 2012。您只需打开 Sql Server 并右键单击您的数据库 -> 将出现下拉菜单 -> 查找 Deattach - 点击该按钮。现在您复制您的 .mdf 文件。现在在第二台计算机/您的客户端计算机中执行以下步骤:- 1) 打开 Sql 服务器。 2)右键单击层次结构格式左上角出现的连接详细信息,将出现一个下拉菜单->查找附加数据库按钮->单击->现在浏览您已从PC分离的.mdf文件,现在它是时候连接到第二台电脑了。点击提交。现在您的数据库将出现在 Connection Hierarchy 中。就是这样。现在开始您的应用程序,如果问题仍然存在,请与我们分享 :) 如果问题已解决,请单击此答案作为已验证/已接受.. :) 我希望此解决方案可以帮助您。

【讨论】:

    【解决方案2】:

    转到您的项目属性。在发布选项卡中打开“应用程序文件”,然后找到您的 mdf 文件并将其发布状态更改为“数据文件(自动)”。

    【讨论】:

      猜你喜欢
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多