Well this i did the below to get the error, don’t have a clue why the database connection fails.
- Create a new ASP.NET Website
- Add a new *.mdf database to App_Data
- Add some tables to it using Server Explorer in Visual Studio
Right click DataBase and Copy Connection string. Insert it into WebConfig File like below
<connectionStrings> <add name="DB" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=C:inetpubwwwrootgsApp_Datadb.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> </connectionStrings>
Add some code to get the data from
selectStatement = "select * from users"; SqlDataAdapter da = new SqlDataAdapter(selectStatement, ConfigurationManager.ConnectionStrings["DB"].ConnectionString); DataTable dtUsers = new DataTable(); da.Fill(dtUsers); GridView1.DataSource = dtUsers.DefaultView; GridView1.DataBind();
and zoot you get the error
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
I have a sneaky suspicion it has to do with permissions. Give full control to your “Authenticated Users”.
In case you are wondering how to do this — I am on Windows 7 and the steps go like this:
- Right-click on the MDF file and click properties.
- Select the “Security” tab and select your “Authenticated Users” (or
something that looks right).
- Click “Edit” and select the “Allow” check-box for “Full Control”.
- OK all the way out.
Just in case if anybody is still looking for solution to this error, this works for me:
1) Open the VStudio project for which
you need to connect to a SQL database
2)Separately, Go to
3) Look for
SQL Server (SQLEXPRESS) service and
4) Start it again
connecting your database now.
Looks like the reason it works has
something to do with User Instance
discussion that is going on in this
I was struggling with this error to and I found that the error was in the database instance that was online so I took it offline from SQLserver management studio,I’ve shared the steps followed and the solution HERE
In my case, I had the database in instance MSSQLSERVER while trying to attach it to SQLEXPRESS. Dropping from the first instance freed the file.
About error: Operating system error 32, Open error …
First off all, give permission to mdf file. In my case NETWORK SERVICE account have FULL ACCESS on data.mdf.
Well, my workspace:
- SSMS have attached data.mdf
- In same time in VS2010 I have open solution with same database file: data.mdf, but can not make successfully connection.
CONNECTION PROPERTIES on
USER INSTANCE change
FALSE and refresh connection inside VS on this database.
Finaly, no more opening error and you have access on same database file in same time from SSMS and VS2010.
Connection string example:
DataSource=.SQLEXPRESS;AttachDbFilename=D:ContractsApp_Datadata.mdf;Integrated Security=True;User Instance=False