日期:2014-05-19  浏览次数:21068 次

C#(WinForm) 获取局域网内的SQL Server服务器名遇到的问题,请帮忙解决
我的代码如下:
SQLDMO.Application   sqlServer   =   new   SQLDMO.ApplicationClass();
                        try
                        {
                                SQLDMO.NameList   serverList   =sqlServer.ListAvailableSQLServers();
                                if   (serverList   !=   null)
                                {
                                        for   (int   i   =   1;   i   <=   serverList.Count;   i++)
                                        {
                                                comboBox1.Items.Add(serverList.Item(i).ToString());
                                        }
                                }
                        }
                        finally
                        {
                                sqlServer   =   null;
                        }
----------
可是提示
Unable   to   cast   COM   object   of   type   'System.__ComObject '   to   interface   type   'SQLDMO.NameList '.   This   operation   failed   because   the   QueryInterface   call   on   the   COM   component   for   the   interface   with   IID   '{10022406-E260-11CF-AE68-00AA004A34D5} '   failed   due   to   the   following   error:   不支持此接口   (Exception   from   HRESULT:   0x80004002   (E_NOINTERFACE)).
--------
好像是一个什么企业服务组件类的接口被定义在另外一个项目中,变更接口时候,导致错误。不知道如何解决?请大家帮忙。我想也有朋友遇到过吧~

------解决方案--------------------
参考下这个看看可以不:
用API得到局域网中可用SqlServer服务器列表
http://blog.csdn.net/hbxtlhx/archive/2005/02/04/280722.aspx
------解决方案--------------------
其他办法就是分析cmd内容,也很简单。不过你需要有osql或sqlcmd命名。
参考:
http://blog.csdn.net/zhzuo/archive/2004/03/21/22024.aspx
http://blog.csdn.net/zhzuo/archive/2006/02/27/611098.aspx