使用.mdf数据库发布ASP.NET项目

时间:2014-04-30 08:18:38

标签: c# asp.net database visual-studio-2010

我有一个ASP.NET项目,它使用存储在App_Data文件夹中的本地.mdf数据库。我通常将本地网站发布到我的桌面,然后获取所有文件并将其上传到asp.net服务器。

成功上传后,由于某些原因无法访问数据库。

我在web.config中包含了数据库的连接字符串

    <connectionStrings> 
    <clear />
      <add name="LocalSqlServer" connectionString="Data Source=(LocalDB)\v11.0;AttachDBFilename=|DataDirectory|database.mdf;database=database;Integrated Security=True;User Instance=False;Context Connection=False;" providerName="System.Data.SqlClient"/>  
      </connectionStrings>

并非我已经看过几个在线解决方案,声明将LocalDB更改为\ v11.0到。\ SQLEXPRESS;在连接字符串中,但这不起作用。

请告知如何使用项目正确上传数据库并设置适当的访问权限。

当我尝试打开数据库连接时,收到以下错误消息

  

System.Security.SecurityException:请求失败。在System.Security.CodeAccessSecurityEngine.Check(对象的需求,StackCrawlMark&安培; stackMark,布尔isPermSet)在System.Security.CodeAccessSecurityEngine.Check(PermissionSet中permSet,StackCrawlMark&安培; stackMark)在System.Security.PermissionSet.Demand()在   System.Data.SqlClient.TdsParser.Connect(SystemInfo serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,System.Data.LocalDBAPI.CreateLocalDBInstance(String instance)中的System.Data.LocalDBAPI.DemandLocalDBPermissions(), System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,SecureString newSecurePassword,Boolean ignoreSniOpenTimeout,TimeoutTimer timeout,Boolean withFailover)at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,String)中的Boolean integratedSecurity,Boolean withFailover NEWPASSWORD,SecureString的newSecurePassword,布尔redirectedUserInstance,SqlConnectionString connectionOptions,SqlCredential凭证,TimeoutTimer超时)在System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer超时,SqlConnectionString connectionOptions,SqlCredential凭证,字符串newPa System.Data.SqlClient上的System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity标识,SqlConnectionString connectionOptions,SqlCredential凭证,Object providerInfo,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString userConnectionOptions)中的ssword,SecureString newSecurePassword,Boolean redirectedUserInstance) .SqlConnectionFactory.CreateConnection在System.Data(DbConnectionOptions选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,池类DBConnectionPool,的DbConnection owningConnection,DbConnectionOptions USEROPTIONS)在System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(池类DBConnectionPool,DbConnectionOptions选项,DbConnectionPoolKey poolKey,DbConnectionOptions USEROPTIONS) System.Data上的System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)中的.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) .ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&amp;连接)在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重试,DbConnectionOptions USEROPTIONS,DbConnectionInternal&安培;连接)在System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory的,TaskCompletionSource在Database.establishConnection()处的System.Data.SqlClient.SqlConnection.Open()处1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1重试)失败的操作是:Demand失败的第一个权限的类型是:System.Security.PermissionSet The Zone失败的程序集是:MyComputer

5 个答案:

答案 0 :(得分:4)

为什么不将数据库发布为脚本而不是.mdf它比使用.mdf更稳定!

使用SQL管理工作室image

答案 1 :(得分:2)

SQLExpress是一个有限的SQL Server版本(2008R2之后的存储容量为10GB,R2之前的存储容量为2-4 gb)

由于它是安全要求的服务器,因此您需要设置密码,用户,角色等。除了这些要求,对于ASP.NET或通常在WWW解决方案上,许多主机提供商都不支持自动连接您的数据库服务器的数据库解决方案..

因此,当您选择在Web项目中使用SQLExpress时,

您可以将创建脚本(创建数据库,表,安全要求,存储过程,视图等等)复制到服务器端并在服务器端运行这些脚本 - 像thealghabban建议的那样 -

或从服务器端手动创建所有需求(提供商具有这些实现的设置页面)

或者向您的提供商询问他们是否允许附加,以决定您的方式

  

{

     

通过附加代码,就像您已尝试进行连接一样   string -with AttachDBFileName语句 - 您需要添加到您的   连接字符串&#34; UserInstance&#34;声明并将其设置为&#34; true&#34;   不是假的!

     

自&#34; UserInstance&#34;从连接字符串中弃用的语句,   您的主机提供商可能不允许使用此声明。   因此,至少对于此弃用,您需要向提供商询问

     

}

如果你仍然想在这里使用SQLExpress,那么你就是WWW连接字符串的一个例子:

Server=SQLServerNameorIPAddressofYourHostProvider\SQLServerInstanceNameofYourHostProvider;Database=yourDatabaseName;User Id=yourUserName;Password=yourPassword;

如果我们假设我们的托管服务提供商是Stackoverflow,那么我们的连接字符串可能是这样:

//证明了stackoverflow.com提供商在公共服务器子域下以及该子域MSSQL2008R2目录下提供数据库服务器

Server=publicservers.stackoverflow.com/MSSQL2008R2\stackExpress;Database=myDB;User Id=theUser;
Password=WhateverPassword;

本地V11几乎是一种新的解决方案(如果我自2011年第4季度以来没有错),它嵌入了像SQLCE(.sdf扩展数据库文件)或旧时尚访问数据库或其他嵌入式解决方案但是它的存储容量有了很大的改进(Access最大2 gb,.sdf db文件最大1 gb):它是SQL Server Express =&gt; 10GB。

要使用本地数据库,如果您的VS版本不是+2012您需要复制所需的Dll,您可以在ProgramFiles目录下找到或在操作系统中安装LocalDB的位置。在VS2012和2013本地数据库中安装VS.但是无论如何要检查你的项目的bin文件是否有本地复制的Local Express ..

对于本地数据库,它的运行时要求你的目标框架至少应该是v 4.0.2 ..检查这个也是..

答案 2 :(得分:0)

发现类似的问题http://www.codeproject.com/Questions/677651/How-to-publish-mdf-database-file-into-a-asp-net-we 它有一个解决方案,希望它有所帮助。

答案 3 :(得分:0)

你可以试试这个。

   string constr=@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\yourdatabase.mdf;Integrated Security=True;User Instance=True;";
   public SqlConnection myConnection = new SqlConnection(constr);

或试试这个

<connectionStrings>

<add name="myConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\yourdatabse.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

</connectionStrings>

我希望能帮助!!

答案 4 :(得分:0)

您当前的数据源(LocalDB)\ v11.0是一个特殊的,孤立的SQLServer 2012实例,名为V11.0,其名称非常奇怪。 (localDb)与您的计算机上的SqlExpresss或任何其他SqlServer的实例不同。但是,即使使用不同版本的SqlServer,它们也可以出现在同一系统上而不会发生冲突。 当您安装SQLExpress或常规SS或更改任何功能时,您可以选择启用LocalDb。您也可以将其作为单独的软件包从MS下载。 可能发生的问题是版本不匹配。我已经看到建议使用(LocalDb)\ MSSQLLocalDB作为通用名称而不是版本号。 关于SqlExpress,名称字符串可能类似于JPLWIN7 \ SQLEXPRESS,其中JPLWIN7是我自己的Windows机器或 本地LAN等机器上的192.168.1.13 \ sqlexpress