2

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. 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.

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.

2 Answers 2

3

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.

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.

See MSDN docs on the ASP.NET SQL Server registration tool for details.

1

• 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. You cannot generate it automatically from ASP.ET Website Administrator. 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. 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). That’s okay. Note: If you want to use a local database in the App_Data directory of your web site, follow these steps. 1. Ensure that that database is not being used by any other application but a single instance of SQL Server Management Studio. 2. Expand the Databases node. 3. Right click on aspnetdb (or whatever you’ve called it) and select 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.
  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).
  5. Paste the database and the log file into this directory.
  6. In SQL Server, right-click on the Databases Node and select Attach.
  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).

  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.

• Manually add the required code to the section of the Visual Studio project’s Web.config file. 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.

SUCCESS!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.