我在SQL Server 2008上有一個數據庫,並試圖在Visual Basic 2010上運行代碼來連接它。我有以下的代碼,但我得到的錯誤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)
上線SQLConn.Open()
:VB2010:連接到SQL Server 2008
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 & " 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
編輯:我一直在努力VBA代碼,並知道服務器\實例和數據庫名稱是否正確。不知道爲什麼它不VB.NET下工作2010
我通過同事一直在研究的VB.NET 2010解決方案進行復制。我在他的電腦上測試過,效果很好。我有權訪問他的SQL Server 2008數據庫,並在第一個TableAdapter.Fill語句中獲得相同的錯誤消息。 我可以通過數據源窗格連接並在設計模式下預覽記錄沒有問題,但一旦它符合它顯然不能。 我應該做一個乾淨的Visual Studio旗艦版安裝還是有補丁/修補程序? – Rick
我已經運行了Visual Studio 2010卸載工具並重新安裝,但仍然收到相同的消息。 – Rick