Is this correct connection string?

shers

Well-known member
Joined
Aug 12, 2007
Messages
86
Programming Experience
1-3
Hi,
I'm working on a multi user VB.Net windows application with back end as SQL Server 2008 Express. The SQL Server is installed on the intranet network drive. This is the connection string I will be using. Is this correct?

VB.NET:
Data Source=.\SQLEXPRESS;AttachDbFilename=Q:\Database\PrintDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
Thanks
 
Sorry, I have changed the connection string to this, as the SQL server is installed in a network server and the database is placed in the local D: drive of the server. This is the connection string.

"Data Source=ServerName\SQLEXPRESS;Database=D:\PrintRequestDatabase\PrintDB. mdf;Trusted_Connection=True;"

The error I get is 'Cannot open database ......'
 
An error message stating you cant connect is a good indication that; your connection string is incorrect... Take a few minutes and read what it says in the help file about connection strings. If your stuck at this point your really going to get stuck actually working with the database. First the word(s) ServerName is supposed to be replaced with.... guess..... the server/pc name that is running the SQL Server software. Since your running SqlExpress I'll assume its on your local pc in which case ServerName/Instance can be replaced with (local). The database iteself should already be attaced to the Sql database so theres no need to repass it the full path to its file, just the database name as the catalog source.


strConnection = "Data Source=(local);Database=PrintDb;Integrated Security=True;"
 
I have installed SQL in my local pc as well as in the server. But I'm trying to connect to the server. Hence the server name is the name of the intranet server.

I changed to PrintDB. Still getting the same error. I think it has got something to do with the SQL Server. I detached the database and then tried to attach it from Management Studio, and here comes the error again. What should I do? Do a repair?
 
Ok I dont know exactly what your new connection string looks like. Your last example included "Data Source=ServerName\SQLEXPRESS;" so I dont know if you replaced that & correctly included your actual server name or not. I'll list a few ways you force a valid connection string to at least compare to see where it differs from your string.

If you "add connection..." in the "Server Explorer" section near your toolbox, a popup connection wizard will appear. Choose your server & database, click the test connection just to verify no problem and then click the advanced button. The advanced dialog will will also open and at the bottom you can see what your properly formatted connection string should be. (see attached screen shot)

Another option is the ConnectionStringBuilder object. With this you just have to correctly add your server & database names, everything else is created for you. If your still unable to connect you can see the problem is one of the values not actually how the string is formatted at least.

VB.NET:
        Dim bldrCon As New SqlConnectionStringBuilder
        Dim con As New SqlConnection

        bldrCon.DataSource = "(local)"
        bldrCon.InitialCatalog = "Pubs"
        bldrCon.IntegratedSecurity = True

        con.ConnectionString = bldrCon.ConnectionString

        Try
            con.Open()
            MessageBox.Show("Connection = " & con.State.ToString)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()
        End Try
 

Attachments

  • image003.jpg
    image003.jpg
    41.2 KB · Views: 43
The Add Connection window is different in my pc. It does not show the Server Name box. That is, I can only connect to the local server. Anyways, I tried to connect to the local server, which worked, then changed the server name to the network server. But in vein.

Thanks
 
Thanks all for your help. I solved it. I think I had messed with the user permission in the database. I deleted the database file and recreated a new one in the remote server. And it works!

Thanks
 

Latest posts

Back
Top