frmconnection.cs

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

CS
76
字号
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 frmConnection : Form
	{
		public frmConnection()
		{
			InitializeComponent();
		}

		private void chkSSPI_CheckedChanged(object sender, EventArgs e)
		{
			bool blnEnableControls = true;

			if (chkSSPI.Checked == true)
			{
				blnEnableControls = false;
			}

			lblPWD.Enabled = blnEnableControls;
			lblUID.Enabled = blnEnableControls;
			txtPwd.Enabled = blnEnableControls;
			txtUID.Enabled = blnEnableControls;

		}

		private void btnConnect_Click(object sender, EventArgs e)
		{
            //ServerConnection object used to connect
            ServerConnection conn = new ServerConnection();
            //pass in the name of the server/instance
            conn.ServerInstance = txtServer.Text.ToString();

            //determine if integrated security is set
			if (chkSSPI.Checked == false)
			{
				//using sql security
                conn.LoginSecure = false;
				conn.Login = txtUID.Text.ToString();
                conn.Password = txtPwd.Text.ToString();				
			}

            //try to connect and return server information
			try
			{				
				Server svrLocal = new Server(conn);
                //pop up the message box to the user here
                //get the info via the Information property
				MessageBox.Show("You are not connected to: " + svrLocal.Name + System.Environment.NewLine +
					" Edition Info: " + svrLocal.Information.Edition + System.Environment.NewLine + 
					" Version Info: " + svrLocal.Information.Version,"SMO Demos");
			}

			catch (SmoException exSMO)
			{
				//catch SMO specific exceptions
                MessageBox.Show(exSMO.ToString());

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

⌨️ 快捷键说明

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