分享
 
 
 

C# WinForm 应用程序的 SQL Server 连接配置界面 动态库

王朝学院·作者佚名  2009-12-04
窄屏简体版  字體: |||超大  

C# WinForm 应用程序的 SQL Server 连接配置界面 动态库

转载请注明来源:http://blog.csdn.net/fengyan19822008/archive/2009/12/03/4929961.aspx

源码已经上传:http://download.csdn.net/source/1868785

作者:Bob 发布日期:2009-12-03

--------------------------------------------------------------------------------

管理系统或数据库应用系统中经常用到的就是数据库连接,程序启动前需要检查数据库连接,环境的更改、软件的重新发布,每次修改app.confige给实施带来很大的麻烦。

模仿VS中添加数据库连接的界面,写了一个自己的动态库。检测配置文件中连接字符串是否正确,如不正确,弹出可视界面配置即可,软件维护或客服人员,只需电话即可完成对客户的指导。

下面是程序运行时的截图:

程序代码如下:

view plaincopy to clipboardprint?

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.cmbSeverName = new System.Windows.Forms.ComboBox();

this.btnReGetServerName = new System.Windows.Forms.Button();

this.groupBox1 = new System.Windows.Forms.GroupBox();

this.chkSavePwd = new System.Windows.Forms.CheckBox();

this.txtPwd = new System.Windows.Forms.TextBox();

this.txtUserName = new System.Windows.Forms.TextBox();

this.lblPwd = new System.Windows.Forms.Label();

this.lblUserName = new System.Windows.Forms.Label();

this.rbQCheck = new System.Windows.Forms.RadioButton();

this.rbWCheck = new System.Windows.Forms.RadioButton();

this.groupBox2 = new System.Windows.Forms.GroupBox();

this.label5 = new System.Windows.Forms.Label();

this.cmbDataBase = new System.Windows.Forms.ComboBox();

this.label6 = new System.Windows.Forms.Label();

this.btnTestConnect = new System.Windows.Forms.Button();

this.btnOK = new System.Windows.Forms.Button();

this.btnCancel = new System.Windows.Forms.Button();

this.groupBox1.SuspendLayout();

this.groupBox2.SuspendLayout();

this.SuspendLayout();

//

// label1

//

this.label1.AutoSize = true;

this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label1.Location = new System.Drawing.Point(16, 15);

this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(205, 20);

this.label1.TabIndex = 0;

this.label1.Text = "输入信息以连接到选定的数据源";

//

// label2

//

this.label2.AutoSize = true;

this.label2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label2.Location = new System.Drawing.Point(17, 46);

this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(97, 20);

this.label2.TabIndex = 1;

this.label2.Text = "服务器名(&E):";

//

// cmbSeverName

//

this.cmbSeverName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.cmbSeverName.FormattingEnabled = true;

this.cmbSeverName.Location = new System.Drawing.Point(22, 72);

this.cmbSeverName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.cmbSeverName.Name = "cmbSeverName";

this.cmbSeverName.Size = new System.Drawing.Size(343, 28);

this.cmbSeverName.TabIndex = 2;

this.cmbSeverName.SelectedValueChanged += new System.EventHandler(this.cmbSeverName_SelectedValueChanged);

this.cmbSeverName.DropDown += new System.EventHandler(this.cmbSeverName_DropDown);

this.cmbSeverName.Click += new System.EventHandler(this.cmbSeverName_Click);

//

// btnReGetServerName

//

this.btnReGetServerName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnReGetServerName.Location = new System.Drawing.Point(374, 72);

this.btnReGetServerName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnReGetServerName.Name = "btnReGetServerName";

this.btnReGetServerName.Size = new System.Drawing.Size(109, 28);

this.btnReGetServerName.TabIndex = 3;

this.btnReGetServerName.Text = "刷新(&R)";

this.btnReGetServerName.UseVisualStyleBackColor = true;

this.btnReGetServerName.Click += new System.EventHandler(this.btnReGetServerName_Click);

//

// groupBox1

//

this.groupBox1.Controls.Add(this.chkSavePwd);

this.groupBox1.Controls.Add(this.txtPwd);

this.groupBox1.Controls.Add(this.txtUserName);

this.groupBox1.Controls.Add(this.lblPwd);

this.groupBox1.Controls.Add(this.lblUserName);

this.groupBox1.Controls.Add(this.rbQCheck);

this.groupBox1.Controls.Add(this.rbWCheck);

this.groupBox1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.groupBox1.Location = new System.Drawing.Point(23, 110);

this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox1.Name = "groupBox1";

this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox1.Size = new System.Drawing.Size(460, 224);

this.groupBox1.TabIndex = 4;

this.groupBox1.TabStop = false;

this.groupBox1.Text = "登陆到服务器";

//

// chkSavePwd

//

this.chkSavePwd.AutoSize = true;

this.chkSavePwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.chkSavePwd.Location = new System.Drawing.Point(121, 190);

this.chkSavePwd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.chkSavePwd.Name = "chkSavePwd";

this.chkSavePwd.Size = new System.Drawing.Size(84, 24);

this.chkSavePwd.TabIndex = 5;

this.chkSavePwd.Text = "保存密码";

this.chkSavePwd.UseVisualStyleBackColor = true;

this.chkSavePwd.Visible = false;

//

// txtPwd

//

this.txtPwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.txtPwd.Location = new System.Drawing.Point(121, 154);

this.txtPwd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.txtPwd.Name = "txtPwd";

this.txtPwd.Size = new System.Drawing.Size(329, 26);

this.txtPwd.TabIndex = 4;

this.txtPwd.UseSystemPasswordChar = true;

//

// txtUserName

//

this.txtUserName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.txtUserName.Location = new System.Drawing.Point(121, 118);

this.txtUserName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.txtUserName.Name = "txtUserName";

this.txtUserName.Size = new System.Drawing.Size(329, 26);

this.txtUserName.TabIndex = 3;

this.txtUserName.TextChanged += new System.EventHandler(this.txtUserName_TextChanged);

this.txtUserName.Validated += new System.EventHandler(this.txtUserName_Validated);

//

// lblPwd

//

this.lblPwd.AutoSize = true;

this.lblPwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.lblPwd.Location = new System.Drawing.Point(43, 157);

this.lblPwd.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.lblPwd.Name = "lblPwd";

this.lblPwd.Size = new System.Drawing.Size(70, 20);

this.lblPwd.TabIndex = 2;

this.lblPwd.Text = "密码(&P):";

//

// lblUserName

//

this.lblUserName.AutoSize = true;

this.lblUserName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.lblUserName.Location = new System.Drawing.Point(28, 121);

this.lblUserName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.lblUserName.Name = "lblUserName";

this.lblUserName.Size = new System.Drawing.Size(85, 20);

this.lblUserName.TabIndex = 2;

this.lblUserName.Text = "用户名(&U):";

//

// rbQCheck

//

this.rbQCheck.AutoSize = true;

this.rbQCheck.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.rbQCheck.Location = new System.Drawing.Point(14, 78);

this.rbQCheck.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.rbQCheck.Name = "rbQCheck";

this.rbQCheck.Size = new System.Drawing.Size(203, 24);

this.rbQCheck.TabIndex = 1;

this.rbQCheck.Text = "使用SQL Server身份验证(&Q)";

this.rbQCheck.UseVisualStyleBackColor = true;

this.rbQCheck.CheckedChanged += new System.EventHandler(this.rbQCheck_CheckedChanged);

//

// rbWCheck

//

this.rbWCheck.AutoSize = true;

this.rbWCheck.Checked = true;

this.rbWCheck.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.rbWCheck.Location = new System.Drawing.Point(14, 44);

this.rbWCheck.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.rbWCheck.Name = "rbWCheck";

this.rbWCheck.Size = new System.Drawing.Size(197, 24);

this.rbWCheck.TabIndex = 0;

this.rbWCheck.TabStop = true;

this.rbWCheck.Text = "使用Windows身份验证(&W)";

this.rbWCheck.UseVisualStyleBackColor = true;

this.rbWCheck.CheckedChanged += new System.EventHandler(this.rbw_CheckedChanged);

//

// groupBox2

//

this.groupBox2.Controls.Add(this.label5);

this.groupBox2.Controls.Add(this.cmbDataBase);

this.groupBox2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.groupBox2.Location = new System.Drawing.Point(26, 344);

this.groupBox2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox2.Name = "groupBox2";

this.groupBox2.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox2.Size = new System.Drawing.Size(460, 106);

this.groupBox2.TabIndex = 5;

this.groupBox2.TabStop = false;

this.groupBox2.Text = "连接到一个数据库";

//

// label5

//

this.label5.AutoSize = true;

this.label5.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label5.Location = new System.Drawing.Point(28, 33);

this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(201, 20);

this.label5.TabIndex = 0;

this.label5.Text = "选择或输入一个数据库名称(&D):";

//

// cmbDataBase

//

this.cmbDataBase.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.cmbDataBase.FormattingEnabled = true;

this.cmbDataBase.Location = new System.Drawing.Point(29, 58);

this.cmbDataBase.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.cmbDataBase.Name = "cmbDataBase";

this.cmbDataBase.Size = new System.Drawing.Size(418, 28);

this.cmbDataBase.TabIndex = 2;

this.cmbDataBase.DropDown += new System.EventHandler(this.cmbDataBase_DropDown);

//

// label6

//

this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.label6.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label6.Location = new System.Drawing.Point(26, 470);

this.label6.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label6.Name = "label6";

this.label6.Size = new System.Drawing.Size(463, 3);

this.label6.TabIndex = 6;

//

// btnTestConnect

//

this.btnTestConnect.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnTestConnect.Location = new System.Drawing.Point(27, 493);

this.btnTestConnect.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnTestConnect.Name = "btnTestConnect";

this.btnTestConnect.Size = new System.Drawing.Size(149, 32);

this.btnTestConnect.TabIndex = 7;

this.btnTestConnect.Text = "测试连接";

this.btnTestConnect.UseVisualStyleBackColor = true;

this.btnTestConnect.Click += new System.EventHandler(this.btnTestConnect_Click);

//

// btnOK

//

this.btnOK.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnOK.Location = new System.Drawing.Point(281, 493);

this.btnOK.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnOK.Name = "btnOK";

this.btnOK.Size = new System.Drawing.Size(98, 32);

this.btnOK.TabIndex = 8;

this.btnOK.Text = "确定";

this.btnOK.UseVisualStyleBackColor = true;

this.btnOK.Click += new System.EventHandler(this.btnOK_Click);

//

// btnCancel

//

this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnCancel.Location = new System.Drawing.Point(387, 493);

this.btnCancel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnCancel.Name = "btnCancel";

this.btnCancel.Size = new System.Drawing.Size(100, 32);

this.btnCancel.TabIndex = 9;

this.btnCancel.Text = "取消";

this.btnCancel.UseVisualStyleBackColor = true;

this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);

//

// frmDataBaseConnect

//

this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(496, 544);

this.Controls.Add(this.btnCancel);

this.Controls.Add(this.btnOK);

this.Controls.Add(this.btnTestConnect);

this.Controls.Add(this.label6);

this.Controls.Add(this.groupBox2);

this.Controls.Add(this.groupBox1);

this.Controls.Add(this.btnReGetServerName);

this.Controls.Add(this.cmbSeverName);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;

this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.Name = "frmDataBaseConnect";

this.Text = "更改连接";

this.Load += new System.EventHandler(this.frmDataBaseConnect_Load);

this.groupBox1.ResumeLayout(false);

this.groupBox1.PerformLayout();

this.groupBox2.ResumeLayout(false);

this.groupBox2.PerformLayout();

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.ComboBox cmbSeverName;

private System.Windows.Forms.Button btnReGetServerName;

private System.Windows.Forms.GroupBox groupBox1;

private System.Windows.Forms.RadioButton rbQCheck;

private System.Windows.Forms.RadioButton rbWCheck;

private System.Windows.Forms.Label lblPwd;

private System.Windows.Forms.Label lblUserName;

private System.Windows.Forms.TextBox txtPwd;

private System.Windows.Forms.TextBox txtUserName;

private System.Windows.Forms.CheckBox chkSavePwd;

private System.Windows.Forms.GroupBox groupBox2;

private System.Windows.Forms.Label label5;

private System.Windows.Forms.ComboBox cmbDataBase;

private System.Windows.Forms.Label label6;

private System.Windows.Forms.Button btnTestConnect;

private System.Windows.Forms.Button btnOK;

private System.Windows.Forms.Button btnCancel;

}

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.cmbSeverName = new System.Windows.Forms.ComboBox();

this.btnReGetServerName = new System.Windows.Forms.Button();

this.groupBox1 = new System.Windows.Forms.GroupBox();

this.chkSavePwd = new System.Windows.Forms.CheckBox();

this.txtPwd = new System.Windows.Forms.TextBox();

this.txtUserName = new System.Windows.Forms.TextBox();

this.lblPwd = new System.Windows.Forms.Label();

this.lblUserName = new System.Windows.Forms.Label();

this.rbQCheck = new System.Windows.Forms.RadioButton();

this.rbWCheck = new System.Windows.Forms.RadioButton();

this.groupBox2 = new System.Windows.Forms.GroupBox();

this.label5 = new System.Windows.Forms.Label();

this.cmbDataBase = new System.Windows.Forms.ComboBox();

this.label6 = new System.Windows.Forms.Label();

this.btnTestConnect = new System.Windows.Forms.Button();

this.btnOK = new System.Windows.Forms.Button();

this.btnCancel = new System.Windows.Forms.Button();

this.groupBox1.SuspendLayout();

this.groupBox2.SuspendLayout();

this.SuspendLayout();

//

// label1

//

this.label1.AutoSize = true;

this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label1.Location = new System.Drawing.Point(16, 15);

this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(205, 20);

this.label1.TabIndex = 0;

this.label1.Text = "输入信息以连接到选定的数据源";

//

// label2

//

this.label2.AutoSize = true;

this.label2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label2.Location = new System.Drawing.Point(17, 46);

this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(97, 20);

this.label2.TabIndex = 1;

this.label2.Text = "服务器名(&E):";

//

// cmbSeverName

//

this.cmbSeverName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.cmbSeverName.FormattingEnabled = true;

this.cmbSeverName.Location = new System.Drawing.Point(22, 72);

this.cmbSeverName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.cmbSeverName.Name = "cmbSeverName";

this.cmbSeverName.Size = new System.Drawing.Size(343, 28);

this.cmbSeverName.TabIndex = 2;

this.cmbSeverName.SelectedValueChanged += new System.EventHandler(this.cmbSeverName_SelectedValueChanged);

this.cmbSeverName.DropDown += new System.EventHandler(this.cmbSeverName_DropDown);

this.cmbSeverName.Click += new System.EventHandler(this.cmbSeverName_Click);

//

// btnReGetServerName

//

this.btnReGetServerName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnReGetServerName.Location = new System.Drawing.Point(374, 72);

this.btnReGetServerName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnReGetServerName.Name = "btnReGetServerName";

this.btnReGetServerName.Size = new System.Drawing.Size(109, 28);

this.btnReGetServerName.TabIndex = 3;

this.btnReGetServerName.Text = "刷新(&R)";

this.btnReGetServerName.UseVisualStyleBackColor = true;

this.btnReGetServerName.Click += new System.EventHandler(this.btnReGetServerName_Click);

//

// groupBox1

//

this.groupBox1.Controls.Add(this.chkSavePwd);

this.groupBox1.Controls.Add(this.txtPwd);

this.groupBox1.Controls.Add(this.txtUserName);

this.groupBox1.Controls.Add(this.lblPwd);

this.groupBox1.Controls.Add(this.lblUserName);

this.groupBox1.Controls.Add(this.rbQCheck);

this.groupBox1.Controls.Add(this.rbWCheck);

this.groupBox1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.groupBox1.Location = new System.Drawing.Point(23, 110);

this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox1.Name = "groupBox1";

this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox1.Size = new System.Drawing.Size(460, 224);

this.groupBox1.TabIndex = 4;

this.groupBox1.TabStop = false;

this.groupBox1.Text = "登陆到服务器";

//

// chkSavePwd

//

this.chkSavePwd.AutoSize = true;

this.chkSavePwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.chkSavePwd.Location = new System.Drawing.Point(121, 190);

this.chkSavePwd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.chkSavePwd.Name = "chkSavePwd";

this.chkSavePwd.Size = new System.Drawing.Size(84, 24);

this.chkSavePwd.TabIndex = 5;

this.chkSavePwd.Text = "保存密码";

this.chkSavePwd.UseVisualStyleBackColor = true;

this.chkSavePwd.Visible = false;

//

// txtPwd

//

this.txtPwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.txtPwd.Location = new System.Drawing.Point(121, 154);

this.txtPwd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.txtPwd.Name = "txtPwd";

this.txtPwd.Size = new System.Drawing.Size(329, 26);

this.txtPwd.TabIndex = 4;

this.txtPwd.UseSystemPasswordChar = true;

//

// txtUserName

//

this.txtUserName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.txtUserName.Location = new System.Drawing.Point(121, 118);

this.txtUserName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.txtUserName.Name = "txtUserName";

this.txtUserName.Size = new System.Drawing.Size(329, 26);

this.txtUserName.TabIndex = 3;

this.txtUserName.TextChanged += new System.EventHandler(this.txtUserName_TextChanged);

this.txtUserName.Validated += new System.EventHandler(this.txtUserName_Validated);

//

// lblPwd

//

this.lblPwd.AutoSize = true;

this.lblPwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.lblPwd.Location = new System.Drawing.Point(43, 157);

this.lblPwd.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.lblPwd.Name = "lblPwd";

this.lblPwd.Size = new System.Drawing.Size(70, 20);

this.lblPwd.TabIndex = 2;

this.lblPwd.Text = "密码(&P):";

//

// lblUserName

//

this.lblUserName.AutoSize = true;

this.lblUserName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.lblUserName.Location = new System.Drawing.Point(28, 121);

this.lblUserName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.lblUserName.Name = "lblUserName";

this.lblUserName.Size = new System.Drawing.Size(85, 20);

this.lblUserName.TabIndex = 2;

this.lblUserName.Text = "用户名(&U):";

//

// rbQCheck

//

this.rbQCheck.AutoSize = true;

this.rbQCheck.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.rbQCheck.Location = new System.Drawing.Point(14, 78);

this.rbQCheck.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.rbQCheck.Name = "rbQCheck";

this.rbQCheck.Size = new System.Drawing.Size(203, 24);

this.rbQCheck.TabIndex = 1;

this.rbQCheck.Text = "使用SQL Server身份验证(&Q)";

this.rbQCheck.UseVisualStyleBackColor = true;

this.rbQCheck.CheckedChanged += new System.EventHandler(this.rbQCheck_CheckedChanged);

//

// rbWCheck

//

this.rbWCheck.AutoSize = true;

this.rbWCheck.Checked = true;

this.rbWCheck.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.rbWCheck.Location = new System.Drawing.Point(14, 44);

this.rbWCheck.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.rbWCheck.Name = "rbWCheck";

this.rbWCheck.Size = new System.Drawing.Size(197, 24);

this.rbWCheck.TabIndex = 0;

this.rbWCheck.TabStop = true;

this.rbWCheck.Text = "使用Windows身份验证(&W)";

this.rbWCheck.UseVisualStyleBackColor = true;

this.rbWCheck.CheckedChanged += new System.EventHandler(this.rbw_CheckedChanged);

//

// groupBox2

//

this.groupBox2.Controls.Add(this.label5);

this.groupBox2.Controls.Add(this.cmbDataBase);

this.groupBox2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.groupBox2.Location = new System.Drawing.Point(26, 344);

this.groupBox2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox2.Name = "groupBox2";

this.groupBox2.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.groupBox2.Size = new System.Drawing.Size(460, 106);

this.groupBox2.TabIndex = 5;

this.groupBox2.TabStop = false;

this.groupBox2.Text = "连接到一个数据库";

//

// label5

//

this.label5.AutoSize = true;

this.label5.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label5.Location = new System.Drawing.Point(28, 33);

this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(201, 20);

this.label5.TabIndex = 0;

this.label5.Text = "选择或输入一个数据库名称(&D):";

//

// cmbDataBase

//

this.cmbDataBase.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.cmbDataBase.FormattingEnabled = true;

this.cmbDataBase.Location = new System.Drawing.Point(29, 58);

this.cmbDataBase.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.cmbDataBase.Name = "cmbDataBase";

this.cmbDataBase.Size = new System.Drawing.Size(418, 28);

this.cmbDataBase.TabIndex = 2;

this.cmbDataBase.DropDown += new System.EventHandler(this.cmbDataBase_DropDown);

//

// label6

//

this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.label6.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.label6.Location = new System.Drawing.Point(26, 470);

this.label6.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);

this.label6.Name = "label6";

this.label6.Size = new System.Drawing.Size(463, 3);

this.label6.TabIndex = 6;

//

// btnTestConnect

//

this.btnTestConnect.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnTestConnect.Location = new System.Drawing.Point(27, 493);

this.btnTestConnect.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnTestConnect.Name = "btnTestConnect";

this.btnTestConnect.Size = new System.Drawing.Size(149, 32);

this.btnTestConnect.TabIndex = 7;

this.btnTestConnect.Text = "测试连接";

this.btnTestConnect.UseVisualStyleBackColor = true;

this.btnTestConnect.Click += new System.EventHandler(this.btnTestConnect_Click);

//

// btnOK

//

this.btnOK.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnOK.Location = new System.Drawing.Point(281, 493);

this.btnOK.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnOK.Name = "btnOK";

this.btnOK.Size = new System.Drawing.Size(98, 32);

this.btnOK.TabIndex = 8;

this.btnOK.Text = "确定";

this.btnOK.UseVisualStyleBackColor = true;

this.btnOK.Click += new System.EventHandler(this.btnOK_Click);

//

// btnCancel

//

this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.btnCancel.Location = new System.Drawing.Point(387, 493);

this.btnCancel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.btnCancel.Name = "btnCancel";

this.btnCancel.Size = new System.Drawing.Size(100, 32);

this.btnCancel.TabIndex = 9;

this.btnCancel.Text = "取消";

this.btnCancel.UseVisualStyleBackColor = true;

this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);

//

// frmDataBaseConnect

//

this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(496, 544);

this.Controls.Add(this.btnCancel);

this.Controls.Add(this.btnOK);

this.Controls.Add(this.btnTestConnect);

this.Controls.Add(this.label6);

this.Controls.Add(this.groupBox2);

this.Controls.Add(this.groupBox1);

this.Controls.Add(this.btnReGetServerName);

this.Controls.Add(this.cmbSeverName);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;

this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);

this.Name = "frmDataBaseConnect";

this.Text = "更改连接";

this.Load += new System.EventHandler(this.frmDataBaseConnect_Load);

this.groupBox1.ResumeLayout(false);

this.groupBox1.PerformLayout();

this.groupBox2.ResumeLayout(false);

this.groupBox2.PerformLayout();

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.ComboBox cmbSeverName;

private System.Windows.Forms.Button btnReGetServerName;

private System.Windows.Forms.GroupBox groupBox1;

private System.Windows.Forms.RadioButton rbQCheck;

private System.Windows.Forms.RadioButton rbWCheck;

private System.Windows.Forms.Label lblPwd;

private System.Windows.Forms.Label lblUserName;

private System.Windows.Forms.TextBox txtPwd;

private System.Windows.Forms.TextBox txtUserName;

private System.Windows.Forms.CheckBox chkSavePwd;

private System.Windows.Forms.GroupBox groupBox2;

private System.Windows.Forms.Label label5;

private System.Windows.Forms.ComboBox cmbDataBase;

private System.Windows.Forms.Label label6;

private System.Windows.Forms.Button btnTestConnect;

private System.Windows.Forms.Button btnOK;

private System.Windows.Forms.Button btnCancel;

}

view plaincopy to clipboardprint?

public partial class frmDataBaseConnect : Form

{

private string strCheckType = ""; //验证方式,Windows验证还是SQL验证

private string strUserName = ";User ID="; //用户名

private string strPwd = ";Password="; //密码

private string strDataBaseName = ";Initial Catalog="; //数据库名称

private string strDataSource = "Data Source="; //服务器名称

private string _strConnetionString = ""; //连接字符串

public string ConnetionString

{

get

{

return _strConnetionString;

}

set { _strConnetionString = value; }

}

public frmDataBaseConnect()

{

InitializeComponent();

}

private void rbQCheck_CheckedChanged(object sender, EventArgs e)

{

}

private void rbw_CheckedChanged(object sender, EventArgs e)

{

if (rbWCheck.Checked)

{

SetEnable(false);

strCheckType = ";Integrated Security=True";

}

else

{

SetEnable(true);

strCheckType = ";Persist Security Info=True";

}

}

//设置连接方式后控件的现实效果

private void SetEnable(bool bEnable)

{

//rbQCheck.Enabled = bEnable;

txtPwd.Enabled = bEnable;

txtUserName.Enabled = bEnable;

chkSavePwd.Enabled = bEnable;

lblPwd.Enabled = bEnable;

lblUserName.Enabled = bEnable;

txtUserNameChange();

}

//获取服务器名称的按钮事件

private void btnReGetServerName_Click(object sender, EventArgs e)

{

btnReGetServerName.Enabled = false;

GetDataBaseSeverName();

btnReGetServerName.Enabled = true;

}

/// <summary>

/// 获取服务器名称

/// </summary>

private void GetDataBaseSeverName()

{

this.Cursor = Cursors.WaitCursor;

cmbSeverName.Items.Clear();

DataTable table = SmoApplication.EnumAvailableSqlServers(); //<----

foreach (DataRow row in table.Rows)

{

string s = "";

foreach (DataColumn c in table.Columns)

{

s += c.Caption + ": " + row[c] + "\n";

if (c.Caption == "Server")

{

this.cmbSeverName.Items.Add(row[c]);

}

}

}

this.cmbSeverName.SelectedIndex = 0;

this.Cursor = Cursors.Default;

}

/// <summary>

/// 获取服务器对应的数据库名称

/// </summary>

/// <param name="strServerName">服务器名称</param>

/// <param name="strUserName">用户名</param>

/// <param name="strPwd">密码</param>

/// <param name="bIsWindowObscured">是否为Windows验证</param>

/// <returns></returns>

public string[] GetDataBaseList(string strServerName, string strUserName, string strPwd, bool bIsWindowObscured)

{

string[] aTables = null;

Server svr = new Server();

svr.ConnectionContext.ServerInstance = strServerName;

svr.ConnectionContext.LoginSecure = bIsWindowObscured;

if (!bIsWindowObscured)

{

svr.ConnectionContext.Login = strUserName;

svr.ConnectionContext.Password = strPwd;

}

try

{

svr.ConnectionContext.Connect();

aTables = new string[svr.Databases.Count];

int i = 0;

foreach (Database db in svr.Databases)

{

aTables[i] = db.Name;

i++;

}

}

catch (System.Exception ex)

{

MessageBox.Show(ex.Message.ToString());

//return

}

return aTables;

}

private void cmbSeverName_Click(object sender, EventArgs e)

{

}

private void frmDataBaseConnect_Load(object sender, EventArgs e)

{

SetEnable(false);

cmbDataBase.Enabled = false;

}

private void txtUserName_Validated(object sender, EventArgs e)

{

}

/// <summary>

/// 用户名不为空时允许“确定”按钮弹出

/// </summary>

private void txtUserNameChange()

{

if (txtUserName.Text.Trim().Length != 0 || rbWCheck.Checked)

{

btnOK.Enabled = true;

}

else

{

btnOK.Enabled = false;

}

}

private void txtUserName_TextChanged(object sender, EventArgs e)

{

txtUserNameChange();

}

private void cmbSeverName_DropDown(object sender, EventArgs e)

{

if (cmbSeverName.Items.Count==0)

{

btnReGetServerName.Enabled = false;

GetDataBaseSeverName();

btnReGetServerName.Enabled = true;

}

if (cmbSeverName.Items.Count!=0)

{

cmbDataBase.Enabled = true;

}

}

/// <summary>

/// 数据库名称下拉框点击时间,获取数据库列表

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void cmbDataBase_DropDown(object sender, EventArgs e)

{

this.Cursor = Cursors.WaitCursor;

cmbDataBase.Items.Clear();

if (cmbSeverName.Text.Trim().Length != 0)

{

try

{

string[] aDataBaseNames = GetDataBaseList(cmbSeverName.Text.Trim().ToString(), txtUserName.Text.Trim().ToString(), txtUserName.Text.Trim().ToString(),rbWCheck.Checked);

if (aDataBaseNames != null)

{

for (int i = 0; i < aDataBaseNames.Length; i++)

{

cmbDataBase.Items.Add(aDataBaseNames[i]);

}

if (cmbDataBase.Items.Count != 0)

{

cmbDataBase.SelectedIndex = 0;

}

}

}

catch (System.Exception ex)

{

MessageBox.Show(ex.Message.ToString(), "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

this.Cursor = Cursors.Default;

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.Close();

}

private void btnTestConnect_Click(object sender, EventArgs e)

{

if (CheckConnect())

{

MessageBox.Show("连接成功!", "连接成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

MessageBox.Show("连接不成功!", "连接不成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

private void cmbSeverName_SelectedValueChanged(object sender, EventArgs e)

{

}

private void btnOK_Click(object sender, EventArgs e)

{

if (!CheckConnect())

{

DialogResult d = MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (d == DialogResult.No)

{

_strConnetionString = "";

this.Close();

}

}

else

{

if (rbWCheck.Checked)

{

//拼写Windows验证的链接字符串

_strConnetionString = strDataSource + cmbSeverName.Text.Trim().ToString() + strDataBaseName + cmbDataBase.Text.Trim().ToString() + ";Integrated Security=True";

}

else

{

//拼写SQL验证的连接字符串

_strConnetionString = strDataSource + cmbSeverName.Text.Trim().ToString()

+ strDataBaseName + cmbDataBase.Text.Trim().ToString() + strCheckType + strUserName

+ txtUserName.Text.Trim().ToString() + strPwd + txtPwd.Text.Trim().ToString();

}

this.Close();

}

}

private bool CheckConnect()

{

bool bResult = false;

//确定按钮点击后,检查是否成功连接,如果成连接,置标志位True,否则为false

//false时,在获取程序连接字符串时提示没有配置成功

string strConnectString;// = ConnetionString;

if (rbWCheck.Checked)

{

//拼写Windows验证的链接字符串

strConnectString = strDataSource + cmbSeverName.Text.Trim().ToString() + strDataBaseName + cmbDataBase.Text.Trim().ToString() + ";Integrated Security=True";

}

else

{

//拼写SQL验证的连接字符串

strConnectString = strDataSource + cmbSeverName.Text.Trim().ToString()

+ strDataBaseName + cmbDataBase.Text.Trim().ToString() + strCheckType + strUserName

+ txtUserName.Text.Trim().ToString() + strPwd + txtPwd.Text.Trim().ToString();

}

SqlConnection myConnection = new SqlConnection(strConnectString);

try

{

myConnection.Open();

//MessageBox.Show("测试连接成功!", "测试连接", MessageBoxButtons.OK, MessageBoxIcon.Information);

bResult = true;

}

catch //(System.Exception ex)

{

//MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

bResult = false;

}

finally

{

myConnection.Close();

}

return bResult;

}

}

public partial class frmDataBaseConnect : Form

{

private string strCheckType = ""; //验证方式,Windows验证还是SQL验证

private string strUserName = ";User ID="; //用户名

private string strPwd = ";Password="; //密码

private string strDataBaseName = ";Initial Catalog="; //数据库名称

private string strDataSource = "Data Source="; //服务器名称

private string _strConnetionString = ""; //连接字符串

public string ConnetionString

{

get

{

return _strConnetionString;

}

set { _strConnetionString = value; }

}

public frmDataBaseConnect()

{

InitializeComponent();

}

private void rbQCheck_CheckedChanged(object sender, EventArgs e)

{

}

private void rbw_CheckedChanged(object sender, EventArgs e)

{

if (rbWCheck.Checked)

{

SetEnable(false);

strCheckType = ";Integrated Security=True";

}

else

{

SetEnable(true);

strCheckType = ";Persist Security Info=True";

}

}

//设置连接方式后控件的现实效果

private void SetEnable(bool bEnable)

{

//rbQCheck.Enabled = bEnable;

txtPwd.Enabled = bEnable;

txtUserName.Enabled = bEnable;

chkSavePwd.Enabled = bEnable;

lblPwd.Enabled = bEnable;

lblUserName.Enabled = bEnable;

txtUserNameChange();

}

//获取服务器名称的按钮事件

private void btnReGetServerName_Click(object sender, EventArgs e)

{

btnReGetServerName.Enabled = false;

GetDataBaseSeverName();

btnReGetServerName.Enabled = true;

}

/// <summary>

/// 获取服务器名称

/// </summary>

private void GetDataBaseSeverName()

{

this.Cursor = Cursors.WaitCursor;

cmbSeverName.Items.Clear();

DataTable table = SmoApplication.EnumAvailableSqlServers(); //<----

foreach (DataRow row in table.Rows)

{

string s = "";

foreach (DataColumn c in table.Columns)

{

s += c.Caption + ": " + row[c] + "\n";

if (c.Caption == "Server")

{

this.cmbSeverName.Items.Add(row[c]);

}

}

}

this.cmbSeverName.SelectedIndex = 0;

this.Cursor = Cursors.Default;

}

/// <summary>

/// 获取服务器对应的数据库名称

/// </summary>

/// <param name="strServerName">服务器名称</param>

/// <param name="strUserName">用户名</param>

/// <param name="strPwd">密码</param>

/// <param name="bIsWindowObscured">是否为Windows验证</param>

/// <returns></returns>

public string[] GetDataBaseList(string strServerName, string strUserName, string strPwd, bool bIsWindowObscured)

{

string[] aTables = null;

Server svr = new Server();

svr.ConnectionContext.ServerInstance = strServerName;

svr.ConnectionContext.LoginSecure = bIsWindowObscured;

if (!bIsWindowObscured)

{

svr.ConnectionContext.Login = strUserName;

svr.ConnectionContext.Password = strPwd;

}

try

{

svr.ConnectionContext.Connect();

aTables = new string[svr.Databases.Count];

int i = 0;

foreach (Database db in svr.Databases)

{

aTables[i] = db.Name;

i++;

}

}

catch (System.Exception ex)

{

MessageBox.Show(ex.Message.ToString());

//return

}

return aTables;

}

private void cmbSeverName_Click(object sender, EventArgs e)

{

}

private void frmDataBaseConnect_Load(object sender, EventArgs e)

{

SetEnable(false);

cmbDataBase.Enabled = false;

}

private void txtUserName_Validated(object sender, EventArgs e)

{

}

/// <summary>

/// 用户名不为空时允许“确定”按钮弹出

/// </summary>

private void txtUserNameChange()

{

if (txtUserName.Text.Trim().Length != 0 || rbWCheck.Checked)

{

btnOK.Enabled = true;

}

else

{

btnOK.Enabled = false;

}

}

private void txtUserName_TextChanged(object sender, EventArgs e)

{

txtUserNameChange();

}

private void cmbSeverName_DropDown(object sender, EventArgs e)

{

if (cmbSeverName.Items.Count==0)

{

btnReGetServerName.Enabled = false;

GetDataBaseSeverName();

btnReGetServerName.Enabled = true;

}

if (cmbSeverName.Items.Count!=0)

{

cmbDataBase.Enabled = true;

}

}

/// <summary>

/// 数据库名称下拉框点击时间,获取数据库列表

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void cmbDataBase_DropDown(object sender, EventArgs e)

{

this.Cursor = Cursors.WaitCursor;

cmbDataBase.Items.Clear();

if (cmbSeverName.Text.Trim().Length != 0)

{

try

{

string[] aDataBaseNames = GetDataBaseList(cmbSeverName.Text.Trim().ToString(), txtUserName.Text.Trim().ToString(), txtUserName.Text.Trim().ToString(),rbWCheck.Checked);

if (aDataBaseNames != null)

{

for (int i = 0; i < aDataBaseNames.Length; i++)

{

cmbDataBase.Items.Add(aDataBaseNames[i]);

}

if (cmbDataBase.Items.Count != 0)

{

cmbDataBase.SelectedIndex = 0;

}

}

}

catch (System.Exception ex)

{

MessageBox.Show(ex.Message.ToString(), "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

this.Cursor = Cursors.Default;

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.Close();

}

private void btnTestConnect_Click(object sender, EventArgs e)

{

if (CheckConnect())

{

MessageBox.Show("连接成功!", "连接成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

MessageBox.Show("连接不成功!", "连接不成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

private void cmbSeverName_SelectedValueChanged(object sender, EventArgs e)

{

}

private void btnOK_Click(object sender, EventArgs e)

{

if (!CheckConnect())

{

DialogResult d = MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (d == DialogResult.No)

{

_strConnetionString = "";

this.Close();

}

}

else

{

if (rbWCheck.Checked)

{

//拼写Windows验证的链接字符串

_strConnetionString = strDataSource + cmbSeverName.Text.Trim().ToString() + strDataBaseName + cmbDataBase.Text.Trim().ToString() + ";Integrated Security=True";

}

else

{

//拼写SQL验证的连接字符串

_strConnetionString = strDataSource + cmbSeverName.Text.Trim().ToString()

+ strDataBaseName + cmbDataBase.Text.Trim().ToString() + strCheckType + strUserName

+ txtUserName.Text.Trim().ToString() + strPwd + txtPwd.Text.Trim().ToString();

}

this.Close();

}

}

private bool CheckConnect()

{

bool bResult = false;

//确定按钮点击后,检查是否成功连接,如果成连接,置标志位True,否则为false

//false时,在获取程序连接字符串时提示没有配置成功

string strConnectString;// = ConnetionString;

if (rbWCheck.Checked)

{

//拼写Windows验证的链接字符串

strConnectString = strDataSource + cmbSeverName.Text.Trim().ToString() + strDataBaseName + cmbDataBase.Text.Trim().ToString() + ";Integrated Security=True";

}

else

{

//拼写SQL验证的连接字符串

strConnectString = strDataSource + cmbSeverName.Text.Trim().ToString()

+ strDataBaseName + cmbDataBase.Text.Trim().ToString() + strCheckType + strUserName

+ txtUserName.Text.Trim().ToString() + strPwd + txtPwd.Text.Trim().ToString();

}

SqlConnection myConnection = new SqlConnection(strConnectString);

try

{

myConnection.Open();

//MessageBox.Show("测试连接成功!", "测试连接", MessageBoxButtons.OK, MessageBoxIcon.Information);

bResult = true;

}

catch //(System.Exception ex)

{

//MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

bResult = false;

}

finally

{

myConnection.Close();

}

return bResult;

}

}

view plaincopy to clipboardprint?

public class classDataBaseOption

{

private INIClass iniCls = new INIClass(AppDomain.CurrentDomain.BaseDirectory + "\\DataBaseConn.ini");

private System.ComponentModel.BackgroundWorker bwShow = new System.ComponentModel.BackgroundWorker();

/// <summary>

/// 是否连接正确

/// </summary>

private bool _bConnected = false;

public bool Connected

{

get { return _bConnected; }

//set{——}

}

/// <summary>

/// 连接字符串

/// </summary>

private string _ConnectionString;

public string ConnectionString

{

get { return _ConnectionString; }

set { _ConnectionString = value; }

}

public classDataBaseOption()

{

GetConnectionString();

}

public classDataBaseOption(bool bResetConnectionString)

{

if (bResetConnectionString)

{

ResetConnectString();

}

//GetConnectionString();

}

/// <summary>

/// 重新设置链接字符串

/// </summary>

private void ResetConnectString()

{

frmDataBaseConnect frmConnect = new frmDataBaseConnect();

frmConnect.ShowDialog();

//_bConnected = frmConnect.

if (frmConnect.ConnetionString == "")

{

_bConnected = false;

}

else

{

//存储新的链接字符串

iniCls.IniWriteValue("ConnectionString", "SqlConnectionString", frmConnect.ConnetionString);

_ConnectionString = frmConnect.ConnetionString;

_bConnected = true;

}

}

//获取连接字符串

private void GetConnectionString()

{

string strConnectionString = iniCls.IniReadValue("ConnectionString", "SqlConnectionString");

if (CheckConnect(strConnectionString))

{

_bConnected = true;

_ConnectionString = strConnectionString;

}

else

{

ResetConnectString();

//_bConnected = true;

}

}

/// <summary>

/// 根据链接字符串测试可连接性

/// </summary>

/// <param name="strConnectionString">连接字符串</param>

/// <returns></returns>

private bool CheckConnect(string strConnectionString)

{

bool bResult = false;

SqlConnection myConnection = new SqlConnection(strConnectionString);

try

{

myConnection.Open();

//MessageBox.Show("测试连接成功!", "测试连接", MessageBoxButtons.OK, MessageBoxIcon.Information);

bResult = true;

}

catch //(System.Exception ex)

{

//MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

bResult = false;

}

finally

{

myConnection.Close();

}

return bResult;

}

}

/// <summary>

/// 读取ini文件

/// </summary>

public class INIClass

{

public string inipath;

[DllImport("kernel32")]

private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

/// <summary>

/// 构造方法

/// </summary>

/// <param name="INIPath">文件路径</param>

public INIClass(string INIPath)

{

inipath = INIPath;

}

/// <summary>

/// 写入INI文件

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

/// <param name="Value">值</param>

public void IniWriteValue(string Section, string Key, string Value)

{

WritePrivateProfileString(Section, Key, Value, this.inipath);

}

/// <summary>

/// 读出INI文件

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

public string IniReadValue(string Section, string Key)

{

StringBuilder temp = new StringBuilder(500);

int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);

return temp.ToString();

}

/// <summary>

/// 验证文件是否存在

/// </summary>

/// <returns>布尔值</returns>

public bool ExistINIFile()

{

return File.Exists(inipath);

}

}

public class classDataBaseOption

{

private INIClass iniCls = new INIClass(AppDomain.CurrentDomain.BaseDirectory + "\\DataBaseConn.ini");

private System.ComponentModel.BackgroundWorker bwShow = new System.ComponentModel.BackgroundWorker();

/// <summary>

/// 是否连接正确

/// </summary>

private bool _bConnected = false;

public bool Connected

{

get { return _bConnected; }

//set{——}

}

/// <summary>

/// 连接字符串

/// </summary>

private string _ConnectionString;

public string ConnectionString

{

get { return _ConnectionString; }

set { _ConnectionString = value; }

}

public classDataBaseOption()

{

GetConnectionString();

}

public classDataBaseOption(bool bResetConnectionString)

{

if (bResetConnectionString)

{

ResetConnectString();

}

//GetConnectionString();

}

/// <summary>

/// 重新设置链接字符串

/// </summary>

private void ResetConnectString()

{

frmDataBaseConnect frmConnect = new frmDataBaseConnect();

frmConnect.ShowDialog();

//_bConnected = frmConnect.

if (frmConnect.ConnetionString == "")

{

_bConnected = false;

}

else

{

//存储新的链接字符串

iniCls.IniWriteValue("ConnectionString", "SqlConnectionString", frmConnect.ConnetionString);

_ConnectionString = frmConnect.ConnetionString;

_bConnected = true;

}

}

//获取连接字符串

private void GetConnectionString()

{

string strConnectionString = iniCls.IniReadValue("ConnectionString", "SqlConnectionString");

if (CheckConnect(strConnectionString))

{

_bConnected = true;

_ConnectionString = strConnectionString;

}

else

{

ResetConnectString();

//_bConnected = true;

}

}

/// <summary>

/// 根据链接字符串测试可连接性

/// </summary>

/// <param name="strConnectionString">连接字符串</param>

/// <returns></returns>

private bool CheckConnect(string strConnectionString)

{

bool bResult = false;

SqlConnection myConnection = new SqlConnection(strConnectionString);

try

{

myConnection.Open();

//MessageBox.Show("测试连接成功!", "测试连接", MessageBoxButtons.OK, MessageBoxIcon.Information);

bResult = true;

}

catch //(System.Exception ex)

{

//MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

bResult = false;

}

finally

{

myConnection.Close();

}

return bResult;

}

}

/// <summary>

/// 读取ini文件

/// </summary>

public class INIClass

{

public string inipath;

[DllImport("kernel32")]

private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

/// <summary>

/// 构造方法

/// </summary>

/// <param name="INIPath">文件路径</param>

public INIClass(string INIPath)

{

inipath = INIPath;

}

/// <summary>

/// 写入INI文件

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

/// <param name="Value">值</param>

public void IniWriteValue(string Section, string Key, string Value)

{

WritePrivateProfileString(Section, Key, Value, this.inipath);

}

/// <summary>

/// 读出INI文件

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

public string IniReadValue(string Section, string Key)

{

StringBuilder temp = new StringBuilder(500);

int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);

return temp.ToString();

}

/// <summary>

/// 验证文件是否存在

/// </summary>

/// <returns>布尔值</returns>

public bool ExistINIFile()

{

return File.Exists(inipath);

}

}

view plaincopy to clipboardprint?

//调用代码

classDataBaseOption clsCheckConnection = new classDataBaseOption();

if (clsCheckConnection.Connected)

{

clsConn.GetConnectionString = clsCheckConnection.ConnectionString;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有