📄 frmserverproperties.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace SMODemos
{
public partial class frmServerProperties : Form
{
public frmServerProperties()
{
InitializeComponent();
}
#region"Helper Functions"
private void ConnectSMO(string strServerName, bool blnIncludeSystemDB)
{
try
{
//use an hourglass
Cursor = Cursors.WaitCursor;
//clear out the items, if you don't do this then
//the combobox will have dupes when you click on
//the show system database box
cboDatabase.Items.Clear();
//this is a reusable function that fills the
//combobox with the available databases on a
//particular server, passing in the cbo by reg
//we will reuse this in a later example
SMOUtilities.FillComboWithDatabases(strServerName, blnIncludeSystemDB, ref cboDatabase);
//now that the cbo is processed we
//will manipulate the UI
if (cboDatabase.Items.Count > 0)
{
lblDatabase.Text = cboDatabase.Items.Count.ToString() + " Database(s) found. Select one:";
cboDatabase.Enabled = true;
}
else
lblDatabase.Text = "No Results";
chkSystemDB.Enabled = true;
}
catch (SmoException exSMO)
{
MessageBox.Show(exSMO.Message.ToString());
SetFormErrorParms();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
SetFormErrorParms();
}
finally
{
Cursor = Cursors.Default;
}
}
private void SetFormErrorParms()
{
lblDatabase.Text = "Errors";
cboDatabase.Enabled = false;
chkSystemDB.Enabled = false;
}
private void ClearForm()
{
cboDatabase.Items.Clear();
propertyGrid1.SelectedObject = null;
cboDatabase.Enabled = false;
chkSystemDB.Enabled = false;
}
#endregion
private void btnConnect_Click(object sender, EventArgs e)
{
//clear the main parts of the form
//using a helper funciton
ClearForm();
ConnectSMO(cboServers.Text, chkSystemDB.Checked);
}
private void comboDB_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//This function will get the database properties
//for selected database in the combobox
Cursor = Cursors.WaitCursor;
Server SelectedServer = new Server(cboServers.Text);
Database SelectedDatabase = SelectedServer.Databases[cboDatabase.Text];
propertyGrid1.SelectedObject =SelectedDatabase;
}
catch (SmoException exSMO)
{
DBPropLabel.Text = "";
MessageBox.Show(exSMO.ToString());
}
catch (Exception ex)
{
DBPropLabel.Text = "";
MessageBox.Show(ex.ToString());
}
finally
{
this.Cursor = Cursors.Default;
}
}
private void comboServer_SelectedIndexChanged(object sender, EventArgs e)
{
ClearForm();
}
private void CheckSystemDB_CheckedChanged(object sender, EventArgs e)
{
ConnectSMO(cboServers.Text, chkSystemDB.Checked);
}
private void button1_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
//get a DataTable of the servers
DataTable dtList = SMOUtilities.ListAllKnownInstances();
foreach (DataRow dr in dtList.Rows)
{
String ServerName;
ServerName = dr["Server"].ToString();
if (dr["Instance"] != null && dr["Instance"].ToString().Length > 0)
ServerName += @"\" + dr["Instance"].ToString();
if (cboServers.Items.IndexOf(ServerName) < 0)
cboServers.Items.Add(ServerName);
}
// By default select the local server
Server LocalServer = new Server();
String LocalServerName = LocalServer.Name;
if (LocalServer.InstanceName != null && LocalServer.InstanceName.Length > 0)
LocalServerName += @"\" + LocalServer.InstanceName;
int intCboIndex = cboServers.FindStringExact(LocalServerName);
if (intCboIndex >= 0)
cboServers.SelectedIndex = intCboIndex;
Cursor = Cursors.Default;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -