none
Can't connect to SQL2008 express from VB2010

    Question

  • I have followed all the examples and searched the internet.  Whenever I try to connect, I get the following error message:

    Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AdventureWorks_Data.mdf". Operating system error 5: "5(Access is denied.)".
    An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AdventureWorks_Data.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    Monday, February 28, 2011 4:42 PM

Answers

  • I have followed all the examples and searched the internet.  Whenever I try to connect, I get the following error message:

    Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AdventureWorks_Data.mdf". Operating system error 5: "5(Access is denied.)".
    An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AdventureWorks_Data.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.


    Hi,

    Could you please elaborate more on your scenario? Have you attached the database file to SQL Server using SQL Server Management Studio Express? How do you access the database? Is there a connection string?

    Based on the error message, it seems that you are trying to re-attach a SQL Server database that has already been attached. A database cannot be attached to multiple SQL Server instances at the same time.

    If you specified a connection string, please modify it as follows:

    Data Source=ServerName;Initial Catalog=DataBase;Integrated Security=SSPI;

    or

    Data Source=ServerName;Initial Catalog=DataBase;User Id=Username;Password=Password;

    If you use a SqlDataSource control or Add Connection wizard of Visual Studio to access the database, please choose Microsoft SQL Server (SqlClient) not Microsoft SQL Server Database File (SqlClient) as the Data Source.

    Microsoft SQL Server (SqlClient) is used to connect to a SQL Server database that has already been attach to a SQL Server instance. Microsoft SQL Server Database File (SqlClient) is used to connect to a SQL Server database without attaching it first. It uses a feature called user instance, which is only supported by SQL Server Express Edition.

    I would also suggest you refer to Papy Normand’s reply for more information about this issue and SQL Server user instance.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by KJian_ Tuesday, March 08, 2011 9:00 AM
    Wednesday, March 02, 2011 2:08 AM

All replies