views:

76

answers:

2

Hi,I'm trying to connect vb 2008 with sql server 2005 locally. I have the database at the same laptop but I always get an error. Here's my code:

Dim strconn As String 
strconn = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Database;Data Source=(local)" 
Try 
    Dim connection As New SqlClient.SqlConnection(strconn) 
    connection.Open() 
Catch ex As Exception 
    MessageBox.Show("failed") 
End Try 

If I run this, its going to prompt "Failed" messagebox. Is there any wrong with the code? What should I do

A: 

You should look at the error details (the ex object) for details on exactly why the connection failed:

MessageBox.Show(ex.ToString())

(I'm a curly-braces person, so my VB might be a little off)

Kragen
it show this: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
vina
@Vina - this means that the `Data Source=(local)` bit is wrong. Can you connect to SQL server using Management Studio? If so copy and paste the value of the "server name" field, for example `Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Database;Data Source=MYMACHINE\SQLEXPRESS`
Kragen
how can i open management studio? but i also have vb 2005 at my laptop, and it can connect to the database using server name localhost. but when i tried it here, it failed.
vina
+1  A: 

The error message you posted clearly says that the server you defined in your connection string isn't available.

Is it possible you might be using a SQL Server Express edition? This gets installed along with Visual Studio 2008, if you don't explicitly uncheck that option in the installer.

The Express editions are installed by default as the SQLExpress instance, so your connection string would have to be something like:

server=(local)\SQLExpress;database=databaseName;integrated security=SSPI; 

in that case.

marc_s
yes, seems like the database can't be find. i don't thing i'm using sql server express edition because when i click start>>all programs>> Microsoft Sql Server 2005. so it's not sql server espress edition, right?
vina