简体   繁体   中英

Changing aspnetdb.mdf from ASP to SQL Server

I am using default ASP.NET membership provider and aspnetdb.mdf in App_Data folder. I then decided to delete aspnetdb.mdf from the App_Data folder and create a new one in SQL Server via aspnet_regsql.exe and modified the connection string accordingly.

Now while checking the role of the user I get an exception error which indicates that it still looks for aspnetdb.mdf at the old path (and not in SQL Server).

This is my code:

void Application_Start(object sender, EventArgs e)
{
        // Code that runs on application startup
        if (!Roles.RoleExists("Administrator"))
        {
            Roles.CreateRole("Administrator");
        }

        if (Membership.GetUser("Admin") == null)
        {
            Membership.CreateUser("Admin", "mrtcn.1907");
            Roles.AddUserToRole("Admin", "Administrator");
        }
}

This is the exception I get:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Web.dll but was not handled in user code

Additional information: An attempt to attach an auto-named database for file C:\\Users\\muratcan\\Documents\\Visual Studio 2013\\Projects\\MedicalBootStrap\\MedicalBootStrap\\App_Data\\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is the connection string;

<add name="ApplicationServices" 
     connectionString="data source=MRTCN-PC\SQLEXPRESS;User Id=medicalusr;Password=123456;AttachDBFilename=|DataDirectory|\aspnetdb.mdf" 
     providerName="System.Data.SqlClient" />

There is most probably another place to be modified accordingly that it looks for the aspnetdb.mdf at the right path.

After Win's suggestion, I have changed the connection string accordingly

This is the modified connection string;

<add name="ApplicationServices" 
     connectionString="data source=MRTCN-PC\SQLEXPRESS;User  Id=medicalusr;Password=123456;AttachDBFilename=aspnetdb.mdf" 
     providerName="System.Data.SqlClient" />

And this is the new exception;

{"An attempt to attach an auto-named database for file aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}

AttachDBFilename=|DataDirectory|\\aspnetdb.mdf means your LocalDB.

If you connect to SQL Server instead of LocalDB, ConnectionString should be something like this -

<connectionStrings>
   <add name="ApplicationServices" 
     connectionString="Data Source=MRTCN-PC\SQLEXPRESS;Initial Catalog=DATABASE_NAME;Persist Security Info=True;User ID=medicalusr;Password=123456" 
     providerName="System.Data.SqlClient"/>
</connectionStrings>

Sample SqlConnection string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM