I have a website that connects to SQLServer Express database files for Membership and data storage.So I have two .mdf files. Here are the connection strings:
public static string ASPNETDB = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;User Instance=True;User ID=;Password=; ";
public static string Dok = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dok.mdf;Integrated Security=True;User Instance=True;User ID=;Password=; ";
Now after deleting and reinstalling the website I've been getting numerous errors in the part where a connection is established.Here are the errors:
Unable to open the physical file "C:\Inetpub\wwwroot\BSHHD\App_Data\aspnetdb_log.ldf".
Operating system error 5: "5(failed to retrieve text for this error. Reason: 1815)".
Cannot open user default database. Login failed.
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
File activation failure. The physical file name "C:\Inetpub\wwwroot\BSHHD\App_Data
\aspnetdb_log.ldf" may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the database
was shutdown, no checkpoint occurred to the database, or the database was read-only.
This error could occur if the transaction log file was manually deleted or lost due to
a hardware or environment failure.
It's very strange because I did not make any changes to the website. I just deleted it from IIS and reinstalled it. I was suggested to set username and password and remove Integrated Security. But I do not know how to set username/password for a datafile.
Add the database ASPNETDB.mdf to the SQL Express with SQL Management studio
You can download it from Microsoft web platform installer.
Open SQL Management studio
Right click on Databases > Attach > Add the mdf and log file (Preferable you put them somewhere else then the site directory.
In MSSQL you can set the username and password easily, Open SQL Management studio.
Login to the Management studio:
Expand Security > Logins > Write Click on Logins "New Login"
Login Name: For example logmein
Make it SQL Server Authentication
Set the Passwords
Remove the Enforce
On the left side you will have the User Mapping
Select the Database you want to connect to and give the write permissions, for example if this user can write to the database you will want to make it datawriter or db_owner which is higher permission:
The database built-in roles are:
public – the default set of authorizations
db_owner – allowed to perform any operation of a specified database
db_oddladmin – allowed to create or modify the new objects of the database (so-called DDL
operations); you should take notice of the fact that the user doesn’t have to have
db_owner authorizations to do that
db_datareader – allowed to read any table
db_datawriter – allowed to write any table
db_denydatareader – banned to read tables (an overt authorization takeaway)
dDb_denydatawriter – banned to write tables
After you add the database to the SQL:
You can add in the web.config
<add name="ASPNETDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=ASPNETDB.mdf;User ID=yourusername;Password=yourpassword" providerName="System.Data.SqlClient" />
<add name="Dok " connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dok.mdf;User ID=yourusername;Password=yourpassword" providerName="System.Data.SqlClient" />
or if you prefer to have it in the code like you have it above also it can work.
Regards,
Gabriel