Saturday, February 25, 2012

Remote connection to SQLEXPRESS doesn't work!

Hello,

OK. It is not possible for me to get remote connection to a ssqlexpress database (error 40). I tried everything posted in this forum, MSDN site etc., I used named pipes, TCP/IP ports, disabled Zone Alarm etc. it is just not working. I can coonect the Northwind database using SQL Server Management Studio Express CTP without any problem. And I am trying to learn creating databases, tables, stored procedures etc. by using code samples given on the MSDN site.

In at least two examples given examples somehow try to use remote connections to SQLEXPRESS databases in the local computer in order to study stored procedures. Since remote connection isn't working I have been trying to do same thing by connecting the database directly not over a network. I do not know how to do it. The part of the code that does the connection is below. Please help. I am stuck.

Athena

The following code is from:

http://msdn2.microsoft.com/en-us/library/5181xbwd(VS.80).aspx

the connection string is:

' Initialize constants for connecting to the database

' and displaying a connection error to the user.

Protected Const SqlConnectionString As String = _

"Server=(local);" & _

"DataBase=;" & _

"Integrated Security=SSPI"

the code:

' This routine executes a SQL statement that deletes the database (if it exists)

' and then creates it.

Private Sub CreateDatabase()

Dim sqlStatement As String = _

"IF EXISTS (" & _

"SELECT * " & _

"FROM master..sysdatabases " & _

"WHERE Name = 'HowToDemo')" & vbCrLf & _

"DROP DATABASE HowToDemo" & vbCrLf & _

"CREATE DATABASE HowToDemo"

' Display a status message saying that we're attempting to connect.

' This only needs to be done the very first time a connection is

' attempted. After we've determined that MSDE or SQL Server is

' installed, this message no longer needs to be displayed.

Dim statusForm As New Status()

If Not didPreviouslyConnect Then

statusForm.Show("Connecting to SQL Server")

End If

' Attempt to connect to the SQL server instance.

Try

' The SqlConnection class allows you to communicate with SQL Server.

' The constructor accepts a connection string as an argument. This

' connection string uses Integrated Security, which means that you

' must have a login in SQL Server, or be part of the Administrators

' group for this to work.

Dim connection As New SqlConnection(connectionString)

' A SqlCommand object is used to execute the SQL commands.

Dim cmd As New SqlCommand(sqlStatement, connection)

' Open the connection, execute the command, and close the

' connection. It is more efficient to ExecuteNonQuery when data is

' not being returned.

connection.Open()

cmd.ExecuteNonQuery()

connection.Close()

' Data has been successfully submitted.

didPreviouslyConnect = True

didCreateTable = True

statusForm.Close()

' Show the controls for the next step.

lblArrow1.Visible = True

lblStep2.Enabled = True

btnCreateTable.Enabled = True

MsgBox("Database 'HowToDemo' successfully created.", MsgBoxStyle.OKOnly, "Database Creation Status")

Catch sqlExc As SqlException

MsgBox(sqlExc.Message, MsgBoxStyle.OKOnly, "SQL Exception Error")

Catch exc As Exception

' Unable to connect to SQL Server or MSDE

statusForm.Close()

MsgBox(exc.Message, MsgBoxStyle.OKOnly, "Connection failed.")

End Try

End Sub

It looks as though your connection string is missing the instance name of SQLEXPRESS

Server=(local)\SQLEXPRESS

Check our our blog post on troubleshooting this here.

http://blogs.msdn.com/sql_protocols/archive/2006/03/23/558651.aspx

hope this helps,

Brad Sarsfield

|||

Thank you Brad. \SQLEXPRESS solved the problem!

If I solve the "The schema returned by the new query differs from the base query" error when I try to create search queries, I can continue to learn SQLServer programming!

Thanks a lot.

Cem

No comments:

Post a Comment