📄 connectform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace KTMIS
{
/// <summary>
/// ConnectForm 的摘要说明。
/// </summary>
public class ConnectForm : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnection;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox tbServer;
private System.Windows.Forms.TextBox tbDataBase;
private System.Windows.Forms.TextBox tbUser;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnDefault;
private System.Windows.Forms.TextBox tbPwd;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public ConnectForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ConnectForm));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.tbServer = new System.Windows.Forms.TextBox();
this.tbDataBase = new System.Windows.Forms.TextBox();
this.tbUser = new System.Windows.Forms.TextBox();
this.tbPwd = new System.Windows.Forms.TextBox();
this.btnConnect = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.btnDefault = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "数据库:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 2;
this.label3.Text = "用户名:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 136);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 23);
this.label4.TabIndex = 3;
this.label4.Text = "密 码:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tbServer
//
this.tbServer.Location = new System.Drawing.Point(72, 16);
this.tbServer.Name = "tbServer";
this.tbServer.Size = new System.Drawing.Size(128, 21);
this.tbServer.TabIndex = 0;
this.tbServer.Text = "";
//
// tbDataBase
//
this.tbDataBase.Location = new System.Drawing.Point(72, 56);
this.tbDataBase.Name = "tbDataBase";
this.tbDataBase.Size = new System.Drawing.Size(128, 21);
this.tbDataBase.TabIndex = 1;
this.tbDataBase.Text = "";
//
// tbUser
//
this.tbUser.Location = new System.Drawing.Point(72, 96);
this.tbUser.Name = "tbUser";
this.tbUser.Size = new System.Drawing.Size(128, 21);
this.tbUser.TabIndex = 2;
this.tbUser.Text = "";
//
// tbPwd
//
this.tbPwd.Location = new System.Drawing.Point(72, 136);
this.tbPwd.Name = "tbPwd";
this.tbPwd.PasswordChar = '*';
this.tbPwd.Size = new System.Drawing.Size(128, 21);
this.tbPwd.TabIndex = 3;
this.tbPwd.Text = "";
//
// btnConnect
//
this.btnConnect.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnConnect.Location = new System.Drawing.Point(216, 16);
this.btnConnect.Name = "btnConnect";
this.btnConnect.TabIndex = 4;
this.btnConnect.Text = "连 接";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// btnReset
//
this.btnReset.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnReset.Location = new System.Drawing.Point(216, 56);
this.btnReset.Name = "btnReset";
this.btnReset.TabIndex = 5;
this.btnReset.Text = "复 位";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// btnDefault
//
this.btnDefault.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnDefault.Location = new System.Drawing.Point(216, 96);
this.btnDefault.Name = "btnDefault";
this.btnDefault.TabIndex = 6;
this.btnDefault.Text = "默 认";
this.btnDefault.Click += new System.EventHandler(this.btnDefault_Click);
//
// btnCancel
//
this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnCancel.Location = new System.Drawing.Point(216, 136);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "取 消";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// ConnectForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 173);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnDefault);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.tbPwd);
this.Controls.Add(this.tbUser);
this.Controls.Add(this.tbDataBase);
this.Controls.Add(this.tbServer);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ConnectForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "连接服务器";
this.Load += new System.EventHandler(this.ConnectForm_Load);
this.ResumeLayout(false);
}
#endregion
private void ConnectForm_Load(object sender, System.EventArgs e)
{
this.btnReset_Click(null, null);
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
string connectionString;
connectionString = "data source =" + this.tbServer.Text.Trim() + ";";
connectionString += "user id =" + this.tbUser.Text.Trim() + ";";
connectionString += "pwd =" + this.tbPwd.Text.Trim() + ";";
connectionString += "initial catalog =" + this.tbDataBase.Text.Trim();
this.sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
try
{
this.sqlConnection.Open();
MessageBox.Show("连接目标服务器成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//this.mainForm.disConnect = false;
this.Close();
}
catch//(Exception caught)
{
this.sqlConnection = null;
MessageBox.Show("连接目标服务器失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.sqlConnection.Close();
}
}
private void btnReset_Click(object sender, System.EventArgs e)
{
this.tbServer.Text = this.tbServer.Tag.ToString();
this.tbDataBase.Text = this.tbDataBase.Tag.ToString();
this.tbUser.Text = this.tbUser.Tag.ToString();
this.tbPwd.Text = this.tbPwd.Tag.ToString();
}
private void btnDefault_Click(object sender, System.EventArgs e)
{
this.tbServer.Text = "(local)";
this.tbDataBase.Text = "KTMIS";
this.tbUser.Text = "sa";
this.tbPwd.Text = "";
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
MessageBox.Show("尚未连接到目标服务器!", "系统警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Close();
}
public void SetConnectStr(string[] connectStr)
{
this.tbServer.Tag = connectStr[0];
this.tbDataBase.Tag = connectStr[1];
this.tbUser.Tag = connectStr[2];
this.tbPwd.Tag = connectStr[3];
}
public void GetConnectStr(ref string[] connectStr)
{
connectStr[0] = this.tbServer.Text.Trim();
connectStr[1] = this.tbUser.Text.Trim();
connectStr[2] = this.tbPwd.Text.Trim();
connectStr[3] = this.tbDataBase.Text.Trim();
}
public System.Data.SqlClient.SqlConnection Connection
{
get
{
return this.sqlConnection;
}
set
{
this.sqlConnection = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -