⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmconnect.cs

📁 可视化的数据库连接与查询,数据库实例列表
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BDCS.DataBaseInformation
{
    public partial class frmConnect : Form
    {
        public frmConnect()
        {
            InitializeComponent();
        }

        public static Microsoft.SqlServer.Management.Smo.Server CurrentServer = new Microsoft.SqlServer.Management.Smo.Server();
        
        public static string ConnectionString = string.Empty;

        private bool FirstClick = false;

        private void frmConnect_Load(object sender, EventArgs e)
        {

        }

        private void EnumServers(bool LocalOnly)
        { 
            System.Data.DataTable dt = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers(LocalOnly);
            if (dt.Rows.Count > 0)
                foreach ( System.Data.DataRow dr in dt.Rows )
                    this.cmbServers.Items.Add(dr["Name"]);
        }

        private void EnumRegisteredServers()
        {
            Microsoft.SqlServer.Management.Smo.RegisteredServers.RegisteredServer[] rses = Microsoft.SqlServer.Management.Smo.SmoApplication.SqlServerRegistrations.EnumRegisteredServers();

            foreach (Microsoft.SqlServer.Management.Smo.RegisteredServers.RegisteredServer rs in rses)
                this.cmbServers.Items.Add(rs.Name);

            rses = null;
        }

        private void chkEmptyPwd_CheckedChanged(object sender, EventArgs e)
        {
            this.txtPwd.Enabled = !this.chkEmptyPwd.Checked;
            this.txtPwd.Text = (this.chkEmptyPwd.Checked) ? string.Empty : this.txtPwd.Text;
        }

        private void radSQLServer_CheckedChanged(object sender, EventArgs e)
        {
            this.txtUser.Enabled = this.radSQLServer.Checked;
            this.txtPwd.Enabled = this.radSQLServer.Checked;
            this.chkEmptyPwd.Enabled = this.radSQLServer.Checked;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.cmbServers.Items.Clear();

            if (this.radRegistered.Checked)
                this.EnumRegisteredServers();
            else
                this.EnumServers(this.radLocal.Checked);
        }

        private void cmbDatabases_Click(object sender, EventArgs e)
        {
            if (this.FirstClick)
                return;

            Microsoft.SqlServer.Management.Common.ServerConnection conn = new Microsoft.SqlServer.Management.Common.ServerConnection();

            if (this.radWindows.Checked)
                conn.LoginSecure = true;
            else
            {
                conn.LoginSecure = false;
                conn.Login = this.txtUser.Text.Trim();
                conn.Password = this.txtPwd.Text;
            }

            conn.Connect();

            if (conn.SqlConnectionObject.State == ConnectionState.Open)
                frmConnect.CurrentServer = new Microsoft.SqlServer.Management.Smo.Server(conn);
            else
                return;

            frmConnect.CurrentServer.ConnectionContext.AutoDisconnectMode = Microsoft.SqlServer.Management.Common.AutoDisconnectMode.NoAutoDisconnect;

            if (frmConnect.CurrentServer.Databases != null)
            {
                if (frmConnect.CurrentServer.Databases.Count > 0)
                {
                    this.cmbDatabases.Items.Clear();
                    foreach (Microsoft.SqlServer.Management.Smo.Database db in frmConnect.CurrentServer.Databases)
                        this.cmbDatabases.Items.Add(db.Name);
                }
            }

            this.FirstClick = true;

        }

        private void btnDBInfo_Click(object sender, EventArgs e)
        {
            if (this.FirstClick)
            {
                if (this.cmbDatabases.Text.Trim() != string.Empty)
                {
                    Microsoft.SqlServer.Management.Smo.Database svrInfo = frmConnect.CurrentServer.Databases[this.cmbDatabases.Text.Trim()];
                    if (svrInfo != null)
                    {
                        frmProperty frm = new frmProperty(svrInfo);
                        frm.Title = svrInfo.Name;
                        frm.ShowDialog();
                    }
                    svrInfo = null;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.FirstClick)
            {
                if (this.cmbDatabases.Text.Trim() != string.Empty)
                {
                    Microsoft.SqlServer.Management.Smo.Database svrInfo = frmConnect.CurrentServer.Databases[this.cmbDatabases.Text.Trim()];
                    if (svrInfo != null)
                    {
                        if (this.radWindows.Checked)
                            frmConnect.ConnectionString = "Data Source=" + this.cmbServers.Text.Trim() + ";Initial Catalog=" + this.cmbDatabases.Text.Trim() + ";Integrated Security=True";
                        else
                            frmConnect.ConnectionString = "Data Source=" + this.cmbServers.Text.Trim() + ";Initial Catalog=" + this.cmbDatabases.Text.Trim() + ";User ID=" + this.txtUser.Text.Trim() + ";Password=" + this.txtPwd.Text.Trim();
                        frmMain frm = new frmMain(this.cmbDatabases.Text.Trim());
                        frm.Show();
                        this.Visible = false;
                    }
                    svrInfo = null;
                }
            }
        }

    }
}

⌨️ 快捷键说明

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