smoutilities.cs

来自「< SQL Server2005程序设计>」· CS 代码 · 共 61 行

CS
61
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace SMODemos
{
    class SMOUtilities
    {
        public static void FillComboWithDatabases(string strServerName, bool blnIncludeSystemDB, ref ComboBox cbo)
        {
            int intDBCount = 0;
            //  Fill the db combo
            Server svr = new Server(strServerName);
            
            foreach (Database db in svr.Databases)
            {
                //determine if the system database
                //flag is set and include/exclude
                if (blnIncludeSystemDB || !db.IsSystemObject)
                {
                    intDBCount++;
                    cbo.Items.Add(db.Name);
                }
            }
        }

public static DataTable ListAllKnownInstances()
{
    //this method will return a DataTable
    //based on the all of the servers avaiable
    //this method can take some time depending on your network
    //will also connect to SQL Server 2000 machines too
    DataTable dtServers = new DataTable();
    try
    {
        
        // Obtain a list of all SQL Server available
        //this method returns a DataTable
        dtServers = SmoApplication.EnumAvailableSqlServers(false);
    }

    catch (SmoException exSMO)
    {
        MessageBox.Show(exSMO.ToString());

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

    return dtServers;
}

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?