English 中文(简体)
• 如何检查SQ是否在遥控机器操作
原标题:How to check if an SQL instance is running in a remote machine

在4个数据库、2个机器(虚拟)和2个其他(虚拟)数据库之间进行Im测试交易复制。

我已经根据以下网址解决了我的问题:question。 但我只是在当地测试。

但是,当我使用该守则进入遥远的机器时,我就获得了未经许可的接受。

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();
    }
}

how can I solve this?

最佳回答

消耗品 服务器的安装是为了听从TCP/IP的连接,我们发现解决这一问题的最容易的方法是用遥远的机器铺设服务器港口。

By ping, I don t mean the traditional command-line ping, I mean opening a connection on the port. Here is some of the VB.Net code that we use to perform this task:

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
问题回答

Do you have permissions to access the remote machine you are trying to connected to? If not use a different overload where you could specify the user credentials

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签