简体   繁体   English

如何从asp.net将aspnetdb.mdf附加到SQL Server 2008 R2的本地实例?

[英]How to attach aspnetdb.mdf to local instance of sql server 2008 R2 from asp.net?

I added a Security Token Service project to my solution using the federation utility. 我使用联合实用程序将安全令牌服务项目添加到了我的解决方案中。 I then added an aspnetdb.mdb file to my project for sqlmembershipprovider store. 然后,我将一个aspnetdb.mdb文件添加到我的sqlmembershipprovider存储项目中。 When I view and test my connection in the Server Explorer my connection succeeds. 当我在服务器资源管理器中查看和测试我的连接时,我的连接成功。 However when I run the solution I get an error: 26, A network-related or instance-specific error occurred while establishing a connection to SQL Server. 但是,当我运行该解决方案时,我得到一个错误:26, 建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。

What am I doing wrong? 我究竟做错了什么? How can I solve this? 我该如何解决?

<add name="connectionstring"
     connectionString="Data Source=.;AttachDbFilename='C:\ClaimsEnableWebsite\ClaimsEnableWebSite_STS\App_Data\ASPNETDB.MDF';Integrated Security=True;User Instance=False;Context Connection=False"
     providerName="System.Data.SqlClient" />

Edit >> Answer: 编辑>>答案:


Connections to SQL Server database files (.mdf) require SQL Server Express to be installed and running on the local computer. 与SQL Server数据库文件(.mdf)的连接要求在本地计算机上安装并运行SQL Server Express。

As you've found out, on any "real" version of SQL Server, you cannot use the AttachDbFileName= approach - this is really only intended for development work using a SQL Server Express edition. 如您AttachDbFileName= ,在任何“真实”版本的SQL Server上,您都不能使用AttachDbFileName=方法-这实际上仅适用于使用SQL Server Express版本的开发工作。

You can however use the ASP.NET SQL Server Registration Tool ( aspnet_regsql ) that's installed with your .NET framework into a folder something like C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319 (substitute your current .NET framework version, if needed) to create those tables in any SQL Server database of your choice. 但是,您可以使用随.NET框架安装的ASP.NET SQL Server注册工具aspnet_regsql )进入C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319类的文件夹(代替您当前的.NET框架版本, (如果需要)在您选择的任何SQL Server数据库中创建这些表。

See MSDN docs on the ASP.NET SQL Server registration tool for details. 有关详细信息,请参见ASP.NET SQL Server注册工具上的MSDN文档

• You must run aspnet_regsql.exe to generate the security database (default aspnetdb) if you are using 'real' SQL Server and not an Express version. •如果使用的是“真实” SQL Server,而不是Express版本,则必须运行aspnet_regsql.exe来生成安全数据库(默认为aspnetdb)。 You cannot generate it automatically from ASP.ET Website Administrator. 您不能从ASP.ET网站管理员自动生成它。 Since the directory where the utility is located is n levels deep, I use a batch file: "c:\\windows\\Microsoft.Net\\Framework\\v4.0.30319"\\aspnet_regsql • The full SQL Server does not allow you to put a database anywhere on the system by default. 由于实用程序所在的目录为n层深,因此我使用了一个批处理文件:“ c:\\ windows \\ Microsoft.Net \\ Framework \\ v4.0.30319” \\ aspnet_regsql•完整的SQL Server不允许您放置数据库默认情况下,系统上的任何位置。 Instead, when you run aspnet_regsql, the aspnetdb.mdf database (or whatever name you give it), is generated in the default directory of Sql Server (under C\\Program Files\\Micrsofot SQL Server...\\Data). 相反,当您运行aspnet_regsql时,将在Sql Server的默认目录(在C \\ Program Files \\ Micrsofot SQL Server ... \\ Data下)中生成aspnetdb.mdf数据库(或您给它的任何名称)。 That's okay. 没关系。 Note: If you want to use a local database in the App_Data directory of your web site, follow these steps. 注意:如果要在网站的App_Data目录中使用本地数据库,请按照下列步骤操作。 1. Ensure that that database is not being used by any other application but a single instance of SQL Server Management Studio. 1.确保该数据库不是由其他任何应用程序使用,而是由SQL Server Management Studio的单个实例使用。 2. Expand the Databases node. 2.展开数据库节点。 3. Right click on aspnetdb (or whatever you've called it) and select Tasks->Detatch. 3.右键单击aspnetdb(或您所说的任何名称),然后选择Tasks-> Detatch。 Note that the Message column should blank, indicating no open connections, or Detach will fail. 请注意,“消息”列应为空白,表示没有打开的连接,否则“分离”将失败。

  1. Click OK to detach the database. 单击确定以分离数据库。
  2. In Windows Explorer, navigate to C”\\Program Files\\Micrsofot SQL Server...\\Data and locate the aspnetdb.mdb and aspnetdb_log.ldf files. 在Windows资源管理器中,导航至C” \\ Program Files \\ Micrsofot SQL Server ... \\ Data,然后找到aspnetdb.mdb和aspnetdb_log.ldf文件。
  3. Cut (or copy) these files from the directory. 从目录中剪切(或复制)这些文件。
  4. In Windows Explorer, navigate to the directory where you want the new database located (the App_Data folder for the website, for example). 在Windows资源管理器中,导航到新数据库所在的目录(例如,网站的App_Data文件夹)。
  5. Paste the database and the log file into this directory. 将数据库和日志文件粘贴到此目录中。
  6. In SQL Server, right-click on the Databases Node and select Attach. 在SQL Server中,右键单击“数据库”节点,然后选择“附加”。
  7. Click the Add button. 单击添加按钮。
  8. Navigate to the directory where you copied the new database and select the mdf (database) file (aspnetdb.mdf, for example). 导航到复制新数据库的目录,然后选择mdf(数据库)文件(例如,aspnetdb.mdf)。

  9. If you want to use one database for security for all of your websites, you can leave the name the same. 如果要使用一个数据库来保护所有网站的安全性,则可以使用相同的名称。 If you want different security databases for different web sites, change the name to a unique name. 如果要为不同的网站使用不同的安全数据库,请将名称更改为唯一名称。

  10. Click OK. 单击确定。
  11. Click OK. 单击确定。
  12. If you right-click on the Databases node and select Refresh, you should see the new security database in Sql Server Management Studio. 如果右键单击“数据库”节点,然后选择“刷新”,则应该在Sql Server Management Studio中看到新的安全数据库。

• Manually add the required code to the section of the Visual Studio project's Web.config file. •手动将所需的代码添加到Visual Studio项目的Web.config文件的部分中。 I have tried hundreds of different variations of this theme, but this seems to be the only one that works every time. 我已经尝试过数百种不同的该主题变体,但这似乎是唯一可以每次使用的变体。

<system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime/>
    <sessionState allowCustomSqlDatabase="true" 
                  sqlConnectionString="Data Source=JAYSDELL\MSSQLSERVER2008R;
                  Initial Catalog=aspnetdb;
                  Integrated Security=True"/>
    <pages controlRenderingCompatibilityVersion="4.0"/>
  </system.web>
  <connectionStrings>
      <remove name="LocalSqlServer"/>
      <add name="LocalSqlServer"
           connectionString="Data Source=JAYSDELL\MSSQLSERVER2008R;
                             Initial Catalog=aspnetdb;
                             Integrated Security=True"
           providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
Obviously values for sqlConnectionString and Initial Catalog need to be tailored to the app.

Now when you go into WEBSET->ASP.NET Configuration->Security you should see the following. 现在,当您进入WEBSET-> ASP.NET Configuration-> Security时,应该看到以下内容。

SUCCESS! 成功!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASPNETDB.MDF中更改用户名 - How to change UserName in ASPNETDB.MDF 使用SQL Server数据库进行登录控制而不是默认的ASPNETDB.mdf数据库 - Using SQL Server database for login control instead of default ASPNETDB.mdf database 如何在Windows Server 2008 R2上安装ASP.net MVC 2? - How to install ASP.net MVC 2 on Windows Server 2008 R2? 如何在sql server 2008 R2中存储.net的DateTime - How to store DateTime of .net in sql server 2008 R2 奇怪的问题:在使用SQL Server 2008 R2的Razor在ASP.NET MVC 3中创建记录时,数据库更新率为60-40 - Weird problem: 60-40 database update rate while creating records in ASP.NET MVC 3 with Razor using SQL Server 2008 R2 WCF和套接字数据处理-异常数据通知模块(ASP.NET MVC 3 + EF 4.0 + LINQ + WCF 4.0 +套接字+ SQL Server 2008 R2 + Azure) - WCF and Socket Data Processing - Anomaly Data Notification Module (ASP.NET MVC 3 + EF 4.0 + LINQ + WCF 4.0 + Sockets + SQL Server 2008 R2 + Azure) SQL Server 2008 R2 - .NET Framework未处理的异常 - SQL Server 2008 R2 - .NET Framework unhandled exception 如何使用sap,sql server 2008 r2和.net框架为.net项目创建设置 - how to create a setup for .net project with sap, sql server 2008 r2 and .net framework SQL Server 2008 R2的可重试SQLBulkCopy - Retryable SQLBulkCopy for SQL Server 2008 R2 检测SQL Server 2008 R2 - Detect SQL Server 2008 R2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM