孟子老大,参照你的做的一个程序,你的程序只能取得计算机名,现在我想取实例名,这个程序只能取得本机的实例名,远程的计算机还是只能取得计算机名,请问怎样才可以取得实例名呀????
try
{
    SQLDMO.NameList sqlServers=null;
    this.cmbServer.Items.Clear();
    //get all available SQL Servers
    SQLDMO.SQLServer2 mSqlServer=new SQLDMO.SQLServer2Class();
    sqlServers=mSqlServer.Application.ListAvailableSQLServers();
    for(int i=0;i<sqlServers.Count;i++)
    {
object srvname = sqlServers.Item( i + 1);
if(srvname != null)
{
         SQLDMO.NameList mInstances=null;
    mInstances=mSqlServer.ListInstalledInstances(srvname);
    int j;
    if(mInstances!=null && mInstances.Count>0)
        for(j=1;j<=mInstances.Count;j++)
    this.cmbServer.Items.Add(mInstances.Item(j as object).ToString());
    else
this.cmbServer.Items.Add(srvname.ToString());
}
    }
    if(cmbServer.Items.Count<1)
    {
MessageBox.Show("没有可用的服务器!!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
    }
    else
this.cmbServer.SelectedIndex=0;
    }
catch(Exception Err)
{
MessageBox.Show(Err.Message,"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}

解决方案 »

  1.   

    http://www.sqldbatips.com/showarticle.asp?ID=45
      

  2.   

    #region 取得区域内的服务器名
    private void GetSQlServer()
    {
    SQLDMO.NameList NameList; 
    SQLDMO.ApplicationClass appsql=new SQLDMO.ApplicationClass();
    int Index;            
    try
    {
    cboServerName.Items.Clear();
    NameList=appsql.ListAvailableSQLServers();

    if(NameList.Count>0)
    {
    for(Index=1;Index<=NameList.Count;Index++)
    {
    if(NameList.Item(Index).Equals("(local)"))
    {
    cboServerName.Items.Add(Environment.MachineName);
    }
    else
    {
    cboServerName.Items.Add(NameList.Item(Index).ToString());
    }
    }
    }

    }
    catch(Exception ex)
    {
    appsql=null;
    NameList=null;
    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
    appsql=null;
    NameList=null;
    }
    }
    #endregion要引用SQLDMO.dll文件
      

  3.   

    孟子老大,那不是用SQLDMO呀.lyb_abiandbel(渴望成为高手) 是我没说清楚,还是你没看清楚哟,我不是要服务器名,服务器名我已经得到了,我要实例名.