title: VB2010: Connect to SQL Server 2008
body: <p>I have a database on SQL Server 2008 and am trying to run code on Visual Basic 2010 to connect to it. I have the following code but am getting the error <code>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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)</code> on the line <code>SQLConn.Open()</code>:</p>
  
  <pre><code>Imports System.Data
  Imports System.Data.SqlClient
  
  Public Class Form1
  
      Dim SQLConn As SqlClient.SqlConnection
  
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          Dim i As Integer
          Dim connectionstring As String
          connectionstring = "Data Source=MySQLServer\MyInstance;Database=MyDatabase;Integrated Security=true;"
  
          Try
              SQLConn = New SqlConnection(connectionstring)
              SQLConn.Open()
          Catch ex As Exception
              MsgBox(ex.Message &amp; " Error Connecting to database!", MsgBoxStyle.Critical)
              Exit Sub
          End Try
          Dim da As SqlDataAdapter
          da = New SqlDataAdapter("SELECT * from DR_Users", SQLConn)
          Dim dt As DataTable
          da.Fill(dt)
          For i = 0 To dt.Rows.Count - 1
              Dim dr As DataRow = dt.Rows(i)
              Debug.Print(dr.Item("UserId").ToString)
          Next
  
      End Sub
  End Class
  </code></pre>
  
  <p>Edit: I have been working on VBA code and know the server\instance and database names are correct. Not sure why it's not working under VB.NET 2010.</p>