Unabel to connect to PUBS database
Whenever I try to connect to the PUBS.MDF database, a login failed error message will appear.
I defined the connection string in the web.config file like this:
Code:
<connectionStrings>
<add name="Pubs" connectionString="Data Source = localhost\SQLEXPRESS; Initial Catalog=Pubs;Integrated Security=SSPI"/>
</connectionStrings>
And setup the connection in the click event of a button
Code:
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("Pubs").ConnectionString
Dim myConnection As New SqlConnection(connectionString)
myConnection.Open()
..........
And I put the PUBS.MDF file in the same folder as the vb source files.
How do I connect to the database correctly?
Re: Unabel to connect to PUBS database
Don't place your MDF in the App_Code folder. It should be in the App_Data folder. Then, to "attach" it, use a connection string like:
Code:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
Re: Unabel to connect to PUBS database
I have tried the above connection string and have placed the database file in the app_data folder but there is still an error.
The message is:
Error reading the databaseDirectory lookup for the file "C:\ADONet\App_Data\PUBS.MDF" failed with the operating system error 5(Access is denied.). Could not attach file 'C:\ADONet\App_Data\PUBS.MDF' as database 'PUBS'.
The connection string I used is:
Code:
<add name="Pubs" connectionString="Data Source = .\SQLEXPRESS;
AttachDBFileName = C:\ADONet\App_Data\PUBS.MDF;Database=PUBS;
Database=PUBS;Integrated Security=SSPI;Trusted_Connection=Yes"/>
:(
Re: Unabel to connect to PUBS database
ok, i've found that when the AttachDBFileName is set to C:\PUBS.MDF, the connection string can work.
But when I use the App_Data folder path, the connection does not work.
What's wrong? :sick:
Re: Unabel to connect to PUBS database
Go to the App_Data folder and check to see if ASPNET or NETWORK SERVICE users have full permissions on it.