我正在测试4个数据库之间的事务复制,2个在一台机器中(虚拟),2个在另一个机器上(也是虚拟的)。
我很难,我已经基于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();
}
}
我该如何解决这个问题?
答案 0 :(得分:1)
您是否有权访问您尝试连接的远程计算机?如果不使用可以指定用户凭据的其他重载
public ManagedComputer(
string machineName,
string userName,
string password
)
答案 1 :(得分: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