使用SMO遍历局域网内指定服务器的数据库列表。
每次可以遍历自己电脑上的数据库,但是不能连接网内的其他服务器。本机和其他电脑都使用的是sa和windows混合验证string a;
a = this.ComboBoxSQLServers.SelectedItem.ToString();
if (a == "SPC100702\\SQLEXPRESS")//本机
          {
            srv.Connect(a, this.textBox_UserName.Text , this.textBox_UserPassWord.Text );
           }
else//其他
          {
             srv.Connect(a, this.textBox_UserName.Text, this.textBox_UserPassWord.Text );
           }
foreach (SQLDMO.Database db in srv.Databases)
         this.ComboBoxDBs.Items.Add(db.Name.ToString());每次连本机外的其他电脑,在else后都出错。哪为高手帮帮忙本人新手,也不知道有没有分给大家

解决方案 »

  1.   

    windowsxp sp3
    sqlserver2005 express
      

  2.   

    你是问的无法获取局域网Sql Server服务器列表?还是能获取但无法连接的问题?
    无法连接可能与防火墙有关,而获取列表却不是每次都能成功的。因为Smo的EnumAvailableSqlServers依赖于超时的Udp广播。
      

  3.   

    各位大侠,我弄错了,是DMO,sqldmo.dll
      

  4.   

    不好意思,弄错了,我引用的是SQLDMO.DLL。能获取服务器列表,但是我不能遍历获取到其他服务器的可用数据库,只能得到自己电脑上的数据库列表
      

  5.   

    看你问题问得是Smo,而你的代码中却写了SQLDMO.Database ,不知你是使用什么?
    我使用smo开发过数据库管理软件。不过用Vb做的,登录界面可以选择是Windows 身份验证还是Sql Server 身份验证。
    给你贴一段VB代码参考:'获取服务器列表
    Public Function GetServerName() As ArrayList
            'Dim ObjServer As Server
            Dim objServers As DataTable
            Dim strServer As String
            Dim arrTmp As New ArrayList
     
            objServers = SmoApplication.EnumAvailableSqlServers()
            For Each objRow As DataRow In objServers.Rows
                strServer = CStr(objRow("Server"))
                If Not TypeOf objRow("Instance") Is DBNull AndAlso CStr(objRow("Instance")).Length > 0 Then
                    strServer += "\" & CStr(objRow("Instance"))
                End If
                arrTmp.Add(strServer)
                'Debug.WriteLine("SQL Server: " & strServer)
            Next
            Return arrTmp
        End Function
    '连接数据库服务器,这是使用windows集成身份验证的。
        Public Overloads Function ConnectServer(ByVal strServer As String) As Boolean
            'Connect to a remote instance of SQL Server.
            Dim s As String
            'The strServer string variable contains the name of a remote instance of SQL Server.        Dim Conn As New ServerConnection()
            Conn.ServerInstance = strServer
            Dim srv As New Server(Conn)
            Try
     '这一句记得当时是为了检测错误而加的(上面的重载方法里是同样的意思),通过访问srv的一个属性获知连接成功与否,失败的话再此过程就引发了错误,会被Catch到。当时纯是权宜之计。这是我两年前做的了,凭印象写此说明。            s = srv.NetName
            Catch ex As ConnectionFailureException
                MsgBox("无法连接到服务器" & strServer & ",请检查用户权限及连接是否可用。", MsgBoxStyle.Exclamation)
                Return False
            End Try
            'The actual connection is made when a property is retrieved. 
            'Console.WriteLine(srv.Information.Version)
            'The connection is automatically disconnected when the Server variable goes out of scope.
            MyServer = srv
            Return True
        End Function
    '连接数据库服务器,这是使用Sql Server 身份验证的。
        Public Overloads Function ConnectServer(ByVal strServer As String, ByVal LogName As String, ByVal Password As String) As Boolean
            Dim s As String
            Dim Conn As New ServerConnection()
            Conn.ServerInstance = strServer
            Conn.LoginSecure = False
            Conn.Login = LogName
            Conn.Password = Password
            Dim srv As New Server(Conn)
            Try
                s = srv.NetName
            Catch ex As ConnectionFailureException
                MsgBox("无法连接到服务器" & strServer & ",请检查用户权限及连接是否可用。", MsgBoxStyle.Exclamation)
                Return False
            End Try        MyServer = Srv
            Return True
        End Function
    自己转成C#吧。
      

  6.   

    不能遍历和Udp广播有关。我当时也是不是每次都能完全遍历到。但是输入服务器名照样可以访问的。
    不过你要注意如果不是默认实例名的话有可能还要输入实例名的(ServerName\InstanceName)。
    当时做项目开始我也用Dmo,后来看了有关资料后发现Dmo落伍了又改用Smo开发了。建议你也用Smo较好。
      

  7.   

    用的是DMO,呵呵
    可以获取到实例名的,就是不能再获取数据库列表了
    srv.Connect(a, this.textBox_UserName.Text, this.textBox_UserPassWord.Text);
    foreach (SQLDMO.Database db in srv.Databases)
      this.ComboBoxDBs.Items.Add(db.Name.ToString());
    第一行运行过去之后没有任何反应,到第二行的时候,srv.Databases就没有任何信息了
      

  8.   

    我可以搜索到局域网内所有的服务器,实例化的。如果“SPC100702\EXPRESS”(本机),"spc100101\express"。但是在遍历第二个服务器的时候,我想得到它上面的所有可用数据库名称,这一步就不行了
      

  9.   

    看来你知道如何获取各对象的方法,我就不再给你贴些代码了。
    不过你使用的是Smo,因为我的那个项目只是开始的时候用的Dmo,后来改为用Smo做了,而且时隔两年。记不太清Dmo的事了。不过建议你也改为用Smo.
    这里我要说的是你描述的“srv.Databases就没有任何信息了”,也许跟我上面注释的捕获错误的方法原因相同吧?也许srv根本就没有成功连接呢?
      

  10.   

    兄弟,谢谢你。问题我已经自己解决了,代码不存在问题,是操作系统的文体。你说的对,DMO确实不常用了。考虑改用SMO