2011-11-17 96 views
2

我正在測試4個數據庫之間的事務複製,2在一臺機器(虛擬)和2個在其他(也是虛擬)。如何檢查SQL實例是否在遠程計算機上運行

我強硬我已經根據這個question解決了我的問題。但我只是在本地進行測試。

但是,當我使用此代碼來訪問遠程機器,我得到一個UnauthorizedAccessException

using Microsoft.SqlServer.Management.Smo.Wmi; 

ManagedComputer mc = new ManagedComputer("169.254.0.16"); 
foreach (Service svc in mc.Services) { 
    if (svc.Name == "MSSQL$SQLEXPRESS"){ 
     textSTW.Text = svc.ServiceState.ToString(); 
    } 
    if (svc.Name == "MSSQL$TESTSERVER"){ 
     textST1.Text = svc.ServiceState.ToString(); 
    } 
} 

我怎樣才能解決這個問題?

+1

運行上述代碼的用戶是否對正在訪問的遠程計算機擁有權限? –

+0

@ ScottA.Lawrence我該如何檢查? – RagnaRock

回答

0

假設SQL Server是建立監聽TCP/IP上,我們發現瞭解決這個問題的最簡單的方法連接ping遠程計算機上的SQL Server端口。

通過ping,我不是指傳統的命令行ping,我的意思是在端口上打開一個連接。以下是我們用來執行此任務的一些VB.Net代碼:

Private Const DEFAULT_SQL_SERVER_PORT As Integer = 1433 

Private m_SocketConnectedEvent As ManualResetEvent 

Private Function PingSQLServer(Optional ByVal wTimeoutSeconds As Integer = -1) As Boolean 
    Dim theSocket As Socket = Nothing 
    Dim theHostEntry As IPHostEntry 
    Dim theEndPoint As IPEndPoint 
    Dim dtStart As Date 
    Dim theTimeSpan As TimeSpan 

    Try 
     ' Wait a second for the status, by default 
     theTimeSpan = New TimeSpan(0, 0, 1) 

     ' Get host related information. 
     theHostEntry = Dns.Resolve(Me.MachineName) 

     ' Loop through the AddressList to obtain the supported AddressFamily. This is to avoid 
     ' an exception that occurs when the host host IP Address is not compatible with the address family 

     For Each theAddress As IPAddress In theHostEntry.AddressList 
      dtStart = Now 

      theEndPoint = Nothing 
      theEndPoint = New IPEndPoint(theAddress, Me.PortNumber) 

      theSocket = Nothing 
      theSocket = New Socket(theEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) 

      m_SocketConnectedEvent = Nothing 
      m_SocketConnectedEvent = New ManualResetEvent(False) 

      ' Wait for a period of time to connect on this IP address/port combo 
      ' If the connect succeeds, get out. 
      theSocket.Blocking = False 
      theSocket.Bind(New IPEndPoint(IPAddress.Any, 0)) 
      theSocket.BeginConnect(theEndPoint, AddressOf ConnectCallback, theSocket) 
      Do 
       If m_SocketConnectedEvent.WaitOne(theTimeSpan, False) Then 
        Exit Do 
       End If 

       Me.IdleDelegate.Invoke(-1, True) 

       If wTimeoutSeconds <> -1 Then 
        If Now.Subtract(dtStart).TotalSeconds >= wTimeoutSeconds Then 
         Try 
          theSocket.Shutdown(SocketShutdown.Both) 
         Catch 
         End Try 
         Try 
          theSocket.Close() 
         Catch 
         End Try 
         theSocket = Nothing 
         Exit Do 
        End If 
       End If 
      Loop 

      If theSocket IsNot Nothing Then 
       If theSocket.Connected Then 
        Return True 
       End If 
      End If 
     Next 

    Catch theException As Exception 
     Call ReportError(theException) 
    Finally 
     If m_SocketConnectedEvent IsNot Nothing Then 
      Try 
       m_SocketConnectedEvent.Close() 
      Catch 
      Finally 
       m_SocketConnectedEvent = Nothing 
      End Try 
     End If 

     If theSocket IsNot Nothing Then 
      Try 
       theSocket.Shutdown(SocketShutdown.Both) 
      Catch 
      End Try 
      Try 
       theSocket.Close() 
      Catch 
      End Try 
      theSocket = Nothing 
     End If 
    End Try 
End Function 

Private Sub ConnectCallback(ByVal ar As IAsyncResult) 
    Try 
     ' Retrieve the socket from the state object. 
     Dim theClient As Socket 

     theClient = DirectCast(ar.AsyncState, Socket) 

     ' Complete the connection. 
     If theClient IsNot Nothing Then 
      theClient.EndConnect(ar) 
     End If 
    Catch theDisposedException As System.ObjectDisposedException 
     ' Ignore this error 
    Catch theSocketException As SocketException 
     Select Case theSocketException.ErrorCode 
      ' Connection actively refused 
      Case 10061 
       ' Don't report the error; it's just that SQL Server isn't answering. 
      Case Else 
       Call ReportError(theSocketException) 
     End Select 
    Catch theException As Exception 
     Call ReportError(theException) 
    Finally 
     ' Signal that the connection has been made. 
     If m_SocketConnectedEvent IsNot Nothing Then 
      m_SocketConnectedEvent.Set() 
     End If 
    End Try 
End Sub 
1

您有權限訪問您嘗試連接的遠程機器嗎?如果不使用不同的過載,你可以指定用戶憑據

public ManagedComputer(
    string machineName, 
    string userName, 
    string password 
) 

http://msdn.microsoft.com/en-us/library/ms194585.aspx

+0

嗯我試過了...得到了同樣的錯誤,在用戶名和密碼我wrinting遠程機器的一個管理員Windows帳戶的用戶名和密碼。 「Virtual2」「密碼」任何想法爲什麼它仍然無法正常工作? – RagnaRock

+0

你還在得到UnauthorizedAccessException嗎?您可能必須使用DomainName \ Username作爲用戶名。還要確保遠程機器上的權限設置正確。有幾件事情可能需要調整。檢查http://msdn.microsoft.com/en-us/library/windows/desktop/aa393266%28v=vs.85%29.aspx –

+0

我的問題更大,因爲這2臺機器不在域中,只是一個工作組 – RagnaRock

相關問題