Results 1 to 5 of 5

Thread: Unable to connect to the Pubs.mdf database file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Red face Unable to connect to the Pubs.mdf database file

    Hi,

    I am using vb2005 to try to connect to the PUB.MDF database file. I copied the file into the same folder as the vb code files.

    I used the statement below as the sqlConnection:

    dim myConnection as SqlConnection = new _
    SqlConnection("server=(local);database=pubs;uid=sa;pwd=")

    When the program runs, there was a run time error and vb highlighted the line "myConnection.open()" and said that there was an error connecting to the database.

    How do I solve this problem?

    I did not set any password and userID to the database file. I tried removing the uid and pwd parameters but the error remains.
    Last edited by LiLo; Dec 8th, 2006 at 02:52 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Unable to connect to the Pubs.mdf database file

    That's not the correct connection string for attaching an MDF file to an SQL Server. That connection string is for connecting to a database contained in the SQL Server itself. If you're trying to attach your own file, or any third party file that hasn't been created by the SQL Server itself, then you need to specify the file by path. See www.connectionstrings.com for various connection string formats. See the SQL Server 2005 section for the correct format to attach an MDF file on connection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Re: [2005] Unable to connect to the Pubs.mdf database file

    I have tried to attach the following connection string from the sql server 2005 section of connectionstrings.com but still there is an error.

    The string is:
    Dim myConnection As SqlConnection = New SqlConnection("Server=.\SQLExpress;AttachDbFilename=C:\Database Files\pubs.mdf;Trusted_Connection=Yes;")

    But the error now is "An attempt to attach an auto-named database for pubs.mdf failed. The specified file cannot be opened"

    When I added the 'Database=dbname' parameter into the connection string, there was also an error about not being able to attach the file with database dbname.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Re: [2005] Unable to connect to the Pubs.mdf database file

    Anyone knows?

  5. #5
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: Unable to connect to the Pubs.mdf database file

    Start a new project with just a blank form.

    Display the Data Source Configation Wizard (Data / Add new Data Source).

    Double-click on the DataBase icon.

    Click on the "New Connection" button.

    On the Add Connection string, Click on the "Change" button and select 'Microsoft SQL Server Database File'

    Click on the "Browse" button and select the database you want.

    Click on the "Test Connection" button to test it.

    Click on the "Advance" button. The connection string is displayed on the button of the "Advanced Properties" screen. You can copy it to the clipboard and paste it into your code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width