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

来源:互联网 发布:如何查看淘宝店铺等级 编辑:程序博客网 时间:2024/05/22 05:11

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

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

 

 

 

程序代码如下:

[c-sharp] view plaincopy
  1. /// <summary>  
  2.         /// 必需的设计器变量。  
  3.         /// </summary>  
  4.         private System.ComponentModel.IContainer components = null;  
  5.   
  6.         /// <summary>  
  7.         /// 清理所有正在使用的资源。  
  8.         /// </summary>  
  9.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
  10.         protected override void Dispose(bool disposing)  
  11.         {  
  12.             if (disposing && (components != null))  
  13.             {  
  14.                 components.Dispose();  
  15.             }  
  16.             base.Dispose(disposing);  
  17.         }  
  18.  
  19.         #region Windows 窗体设计器生成的代码  
  20.   
  21.         /// <summary>  
  22.         /// 设计器支持所需的方法 - 不要  
  23.         /// 使用代码编辑器修改此方法的内容。  
  24.         /// </summary>  
  25.         private void InitializeComponent()  
  26.         {  
  27.             this.label1 = new System.Windows.Forms.Label();  
  28.             this.label2 = new System.Windows.Forms.Label();  
  29.             this.cmbSeverName = new System.Windows.Forms.ComboBox();  
  30.             this.btnReGetServerName = new System.Windows.Forms.Button();  
  31.             this.groupBox1 = new System.Windows.Forms.GroupBox();  
  32.             this.chkSavePwd = new System.Windows.Forms.CheckBox();  
  33.             this.txtPwd = new System.Windows.Forms.TextBox();  
  34.             this.txtUserName = new System.Windows.Forms.TextBox();  
  35.             this.lblPwd = new System.Windows.Forms.Label();  
  36.             this.lblUserName = new System.Windows.Forms.Label();  
  37.             this.rbQCheck = new System.Windows.Forms.RadioButton();  
  38.             this.rbWCheck = new System.Windows.Forms.RadioButton();  
  39.             this.groupBox2 = new System.Windows.Forms.GroupBox();  
  40.             this.label5 = new System.Windows.Forms.Label();  
  41.             this.cmbDataBase = new System.Windows.Forms.ComboBox();  
  42.             this.label6 = new System.Windows.Forms.Label();  
  43.             this.btnTestConnect = new System.Windows.Forms.Button();  
  44.             this.btnOK = new System.Windows.Forms.Button();  
  45.             this.btnCancel = new System.Windows.Forms.Button();  
  46.             this.groupBox1.SuspendLayout();  
  47.             this.groupBox2.SuspendLayout();  
  48.             this.SuspendLayout();  
  49.             //   
  50.             // label1  
  51.             //   
  52.             this.label1.AutoSize = true;  
  53.             this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  54.             this.label1.Location = new System.Drawing.Point(16, 15);  
  55.             this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);  
  56.             this.label1.Name = "label1";  
  57.             this.label1.Size = new System.Drawing.Size(205, 20);  
  58.             this.label1.TabIndex = 0;  
  59.             this.label1.Text = "输入信息以连接到选定的数据源";  
  60.             //   
  61.             // label2  
  62.             //   
  63.             this.label2.AutoSize = true;  
  64.             this.label2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  65.             this.label2.Location = new System.Drawing.Point(17, 46);  
  66.             this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);  
  67.             this.label2.Name = "label2";  
  68.             this.label2.Size = new System.Drawing.Size(97, 20);  
  69.             this.label2.TabIndex = 1;  
  70.             this.label2.Text = "服务器名(&E):";  
  71.             //   
  72.             // cmbSeverName  
  73.             //   
  74.             this.cmbSeverName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  75.             this.cmbSeverName.FormattingEnabled = true;  
  76.             this.cmbSeverName.Location = new System.Drawing.Point(22, 72);  
  77.             this.cmbSeverName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  78.             this.cmbSeverName.Name = "cmbSeverName";  
  79.             this.cmbSeverName.Size = new System.Drawing.Size(343, 28);  
  80.             this.cmbSeverName.TabIndex = 2;  
  81.             this.cmbSeverName.SelectedValueChanged += new System.EventHandler(this.cmbSeverName_SelectedValueChanged);  
  82.             this.cmbSeverName.DropDown += new System.EventHandler(this.cmbSeverName_DropDown);  
  83.             this.cmbSeverName.Click += new System.EventHandler(this.cmbSeverName_Click);  
  84.             //   
  85.             // btnReGetServerName  
  86.             //   
  87.             this.btnReGetServerName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  88.             this.btnReGetServerName.Location = new System.Drawing.Point(374, 72);  
  89.             this.btnReGetServerName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  90.             this.btnReGetServerName.Name = "btnReGetServerName";  
  91.             this.btnReGetServerName.Size = new System.Drawing.Size(109, 28);  
  92.             this.btnReGetServerName.TabIndex = 3;  
  93.             this.btnReGetServerName.Text = "刷新(&R)";  
  94.             this.btnReGetServerName.UseVisualStyleBackColor = true;  
  95.             this.btnReGetServerName.Click += new System.EventHandler(this.btnReGetServerName_Click);  
  96.             //   
  97.             // groupBox1  
  98.             //   
  99.             this.groupBox1.Controls.Add(this.chkSavePwd);  
  100.             this.groupBox1.Controls.Add(this.txtPwd);  
  101.             this.groupBox1.Controls.Add(this.txtUserName);  
  102.             this.groupBox1.Controls.Add(this.lblPwd);  
  103.             this.groupBox1.Controls.Add(this.lblUserName);  
  104.             this.groupBox1.Controls.Add(this.rbQCheck);  
  105.             this.groupBox1.Controls.Add(this.rbWCheck);  
  106.             this.groupBox1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  107.             this.groupBox1.Location = new System.Drawing.Point(23, 110);  
  108.             this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  109.             this.groupBox1.Name = "groupBox1";  
  110.             this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  111.             this.groupBox1.Size = new System.Drawing.Size(460, 224);  
  112.             this.groupBox1.TabIndex = 4;  
  113.             this.groupBox1.TabStop = false;  
  114.             this.groupBox1.Text = "登陆到服务器";  
  115.             //   
  116.             // chkSavePwd  
  117.             //   
  118.             this.chkSavePwd.AutoSize = true;  
  119.             this.chkSavePwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  120.             this.chkSavePwd.Location = new System.Drawing.Point(121, 190);  
  121.             this.chkSavePwd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  122.             this.chkSavePwd.Name = "chkSavePwd";  
  123.             this.chkSavePwd.Size = new System.Drawing.Size(84, 24);  
  124.             this.chkSavePwd.TabIndex = 5;  
  125.             this.chkSavePwd.Text = "保存密码";  
  126.             this.chkSavePwd.UseVisualStyleBackColor = true;  
  127.             this.chkSavePwd.Visible = false;  
  128.             //   
  129.             // txtPwd  
  130.             //   
  131.             this.txtPwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  132.             this.txtPwd.Location = new System.Drawing.Point(121, 154);  
  133.             this.txtPwd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  134.             this.txtPwd.Name = "txtPwd";  
  135.             this.txtPwd.Size = new System.Drawing.Size(329, 26);  
  136.             this.txtPwd.TabIndex = 4;  
  137.             this.txtPwd.UseSystemPasswordChar = true;  
  138.             //   
  139.             // txtUserName  
  140.             //   
  141.             this.txtUserName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  142.             this.txtUserName.Location = new System.Drawing.Point(121, 118);  
  143.             this.txtUserName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  144.             this.txtUserName.Name = "txtUserName";  
  145.             this.txtUserName.Size = new System.Drawing.Size(329, 26);  
  146.             this.txtUserName.TabIndex = 3;  
  147.             this.txtUserName.TextChanged += new System.EventHandler(this.txtUserName_TextChanged);  
  148.             this.txtUserName.Validated += new System.EventHandler(this.txtUserName_Validated);  
  149.             //   
  150.             // lblPwd  
  151.             //   
  152.             this.lblPwd.AutoSize = true;  
  153.             this.lblPwd.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  154.             this.lblPwd.Location = new System.Drawing.Point(43, 157);  
  155.             this.lblPwd.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);  
  156.             this.lblPwd.Name = "lblPwd";  
  157.             this.lblPwd.Size = new System.Drawing.Size(70, 20);  
  158.             this.lblPwd.TabIndex = 2;  
  159.             this.lblPwd.Text = "密码(&P):";  
  160.             //   
  161.             // lblUserName  
  162.             //   
  163.             this.lblUserName.AutoSize = true;  
  164.             this.lblUserName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  165.             this.lblUserName.Location = new System.Drawing.Point(28, 121);  
  166.             this.lblUserName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);  
  167.             this.lblUserName.Name = "lblUserName";  
  168.             this.lblUserName.Size = new System.Drawing.Size(85, 20);  
  169.             this.lblUserName.TabIndex = 2;  
  170.             this.lblUserName.Text = "用户名(&U):";  
  171.             //   
  172.             // rbQCheck  
  173.             //   
  174.             this.rbQCheck.AutoSize = true;  
  175.             this.rbQCheck.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  176.             this.rbQCheck.Location = new System.Drawing.Point(14, 78);  
  177.             this.rbQCheck.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  178.             this.rbQCheck.Name = "rbQCheck";  
  179.             this.rbQCheck.Size = new System.Drawing.Size(203, 24);  
  180.             this.rbQCheck.TabIndex = 1;  
  181.             this.rbQCheck.Text = "使用SQL Server身份验证(&Q)";  
  182.             this.rbQCheck.UseVisualStyleBackColor = true;  
  183.             this.rbQCheck.CheckedChanged += new System.EventHandler(this.rbQCheck_CheckedChanged);  
  184.             //   
  185.             // rbWCheck  
  186.             //   
  187.             this.rbWCheck.AutoSize = true;  
  188.             this.rbWCheck.Checked = true;  
  189.             this.rbWCheck.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  190.             this.rbWCheck.Location = new System.Drawing.Point(14, 44);  
  191.             this.rbWCheck.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  192.             this.rbWCheck.Name = "rbWCheck";  
  193.             this.rbWCheck.Size = new System.Drawing.Size(197, 24);  
  194.             this.rbWCheck.TabIndex = 0;  
  195.             this.rbWCheck.TabStop = true;  
  196.             this.rbWCheck.Text = "使用Windows身份验证(&W)";  
  197.             this.rbWCheck.UseVisualStyleBackColor = true;  
  198.             this.rbWCheck.CheckedChanged += new System.EventHandler(this.rbw_CheckedChanged);  
  199.             //   
  200.             // groupBox2  
  201.             //   
  202.             this.groupBox2.Controls.Add(this.label5);  
  203.             this.groupBox2.Controls.Add(this.cmbDataBase);  
  204.             this.groupBox2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  205.             this.groupBox2.Location = new System.Drawing.Point(26, 344);  
  206.             this.groupBox2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  207.             this.groupBox2.Name = "groupBox2";  
  208.             this.groupBox2.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  209.             this.groupBox2.Size = new System.Drawing.Size(460, 106);  
  210.             this.groupBox2.TabIndex = 5;  
  211.             this.groupBox2.TabStop = false;  
  212.             this.groupBox2.Text = "连接到一个数据库";  
  213.             //   
  214.             // label5  
  215.             //   
  216.             this.label5.AutoSize = true;  
  217.             this.label5.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  218.             this.label5.Location = new System.Drawing.Point(28, 33);  
  219.             this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);  
  220.             this.label5.Name = "label5";  
  221.             this.label5.Size = new System.Drawing.Size(201, 20);  
  222.             this.label5.TabIndex = 0;  
  223.             this.label5.Text = "选择或输入一个数据库名称(&D):";  
  224.             //   
  225.             // cmbDataBase  
  226.             //   
  227.             this.cmbDataBase.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  228.             this.cmbDataBase.FormattingEnabled = true;  
  229.             this.cmbDataBase.Location = new System.Drawing.Point(29, 58);  
  230.             this.cmbDataBase.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  231.             this.cmbDataBase.Name = "cmbDataBase";  
  232.             this.cmbDataBase.Size = new System.Drawing.Size(418, 28);  
  233.             this.cmbDataBase.TabIndex = 2;  
  234.             this.cmbDataBase.DropDown += new System.EventHandler(this.cmbDataBase_DropDown);  
  235.             //   
  236.             // label6  
  237.             //   
  238.             this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;  
  239.             this.label6.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  240.             this.label6.Location = new System.Drawing.Point(26, 470);  
  241.             this.label6.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);  
  242.             this.label6.Name = "label6";  
  243.             this.label6.Size = new System.Drawing.Size(463, 3);  
  244.             this.label6.TabIndex = 6;  
  245.             //   
  246.             // btnTestConnect  
  247.             //   
  248.             this.btnTestConnect.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  249.             this.btnTestConnect.Location = new System.Drawing.Point(27, 493);  
  250.             this.btnTestConnect.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  251.             this.btnTestConnect.Name = "btnTestConnect";  
  252.             this.btnTestConnect.Size = new System.Drawing.Size(149, 32);  
  253.             this.btnTestConnect.TabIndex = 7;  
  254.             this.btnTestConnect.Text = "测试连接";  
  255.             this.btnTestConnect.UseVisualStyleBackColor = true;  
  256.             this.btnTestConnect.Click += new System.EventHandler(this.btnTestConnect_Click);  
  257.             //   
  258.             // btnOK  
  259.             //   
  260.             this.btnOK.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  261.             this.btnOK.Location = new System.Drawing.Point(281, 493);  
  262.             this.btnOK.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  263.             this.btnOK.Name = "btnOK";  
  264.             this.btnOK.Size = new System.Drawing.Size(98, 32);  
  265.             this.btnOK.TabIndex = 8;  
  266.             this.btnOK.Text = "确定";  
  267.             this.btnOK.UseVisualStyleBackColor = true;  
  268.             this.btnOK.Click += new System.EventHandler(this.btnOK_Click);  
  269.             //   
  270.             // btnCancel  
  271.             //   
  272.             this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  273.             this.btnCancel.Location = new System.Drawing.Point(387, 493);  
  274.             this.btnCancel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  275.             this.btnCancel.Name = "btnCancel";  
  276.             this.btnCancel.Size = new System.Drawing.Size(100, 32);  
  277.             this.btnCancel.TabIndex = 9;  
  278.             this.btnCancel.Text = "取消";  
  279.             this.btnCancel.UseVisualStyleBackColor = true;  
  280.             this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);  
  281.             //   
  282.             // frmDataBaseConnect  
  283.             //   
  284.             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);  
  285.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  286.             this.ClientSize = new System.Drawing.Size(496, 544);  
  287.             this.Controls.Add(this.btnCancel);  
  288.             this.Controls.Add(this.btnOK);  
  289.             this.Controls.Add(this.btnTestConnect);  
  290.             this.Controls.Add(this.label6);  
  291.             this.Controls.Add(this.groupBox2);  
  292.             this.Controls.Add(this.groupBox1);  
  293.             this.Controls.Add(this.btnReGetServerName);  
  294.             this.Controls.Add(this.cmbSeverName);  
  295.             this.Controls.Add(this.label2);  
  296.             this.Controls.Add(this.label1);  
  297.             this.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  298.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;  
  299.             this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);  
  300.             this.Name = "frmDataBaseConnect";  
  301.             this.Text = "更改连接";  
  302.             this.Load += new System.EventHandler(this.frmDataBaseConnect_Load);  
  303.             this.groupBox1.ResumeLayout(false);  
  304.             this.groupBox1.PerformLayout();  
  305.             this.groupBox2.ResumeLayout(false);  
  306.             this.groupBox2.PerformLayout();  
  307.             this.ResumeLayout(false);  
  308.             this.PerformLayout();  
  309.   
  310.         }  
  311.  
  312.         #endregion  
  313.   
  314.         private System.Windows.Forms.Label label1;  
  315.         private System.Windows.Forms.Label label2;  
  316.         private System.Windows.Forms.ComboBox cmbSeverName;  
  317.         private System.Windows.Forms.Button btnReGetServerName;  
  318.         private System.Windows.Forms.GroupBox groupBox1;  
  319.         private System.Windows.Forms.RadioButton rbQCheck;  
  320.         private System.Windows.Forms.RadioButton rbWCheck;  
  321.         private System.Windows.Forms.Label lblPwd;  
  322.         private System.Windows.Forms.Label lblUserName;  
  323.         private System.Windows.Forms.TextBox txtPwd;  
  324.         private System.Windows.Forms.TextBox txtUserName;  
  325.         private System.Windows.Forms.CheckBox chkSavePwd;  
  326.         private System.Windows.Forms.GroupBox groupBox2;  
  327.         private System.Windows.Forms.Label label5;  
  328.         private System.Windows.Forms.ComboBox cmbDataBase;  
  329.         private System.Windows.Forms.Label label6;  
  330.         private System.Windows.Forms.Button btnTestConnect;  
  331.         private System.Windows.Forms.Button btnOK;  
  332.         private System.Windows.Forms.Button btnCancel;  
  333.     }  

 

[c-sharp] view plaincopy
  1. public partial class frmDataBaseConnect : Form  
  2. {  
  3.       
  4.     private string strCheckType = "";                       //验证方式,Windows验证还是SQL验证  
  5.     private string strUserName = ";User ID=";               //用户名  
  6.     private string strPwd = ";Password=";                   //密码  
  7.     private string strDataBaseName = ";Initial Catalog=";   //数据库名称  
  8.     private string strDataSource = "Data Source=";          //服务器名称  
  9.       
  10.     private string _strConnetionString = "";                //连接字符串  
  11.     public string ConnetionString  
  12.     {  
  13.         get  
  14.         {  
  15.             return _strConnetionString;  
  16.         }  
  17.         set { _strConnetionString = value; }  
  18.     }  
  19.   
  20.     public frmDataBaseConnect()  
  21.     {  
  22.         InitializeComponent();  
  23.     }  
  24.   
  25.     private void rbQCheck_CheckedChanged(object sender, EventArgs e)  
  26.     {  
  27.   
  28.     }  
  29.   
  30.     private void rbw_CheckedChanged(object sender, EventArgs e)  
  31.     {  
  32.         if (rbWCheck.Checked)  
  33.         {  
  34.             SetEnable(false);  
  35.             strCheckType = ";Integrated Security=True";  
  36.         }  
  37.         else  
  38.         {  
  39.             SetEnable(true);  
  40.             strCheckType = ";Persist Security Info=True";  
  41.         }  
  42.     }  
  43.   
  44.     //设置连接方式后控件的现实效果    
  45.     private void SetEnable(bool bEnable)  
  46.     {  
  47.         //rbQCheck.Enabled = bEnable;  
  48.         txtPwd.Enabled = bEnable;  
  49.         txtUserName.Enabled = bEnable;  
  50.         chkSavePwd.Enabled = bEnable;  
  51.         lblPwd.Enabled = bEnable;  
  52.         lblUserName.Enabled = bEnable;  
  53.         txtUserNameChange();  
  54.     }  
  55.   
  56.     //获取服务器名称的按钮事件  
  57.     private void btnReGetServerName_Click(object sender, EventArgs e)  
  58.     {  
  59.         btnReGetServerName.Enabled = false;  
  60.         GetDataBaseSeverName();  
  61.         btnReGetServerName.Enabled = true;  
  62.     }  
  63.   
  64.     /// <summary>  
  65.     /// 获取服务器名称  
  66.     /// </summary>  
  67.     private void GetDataBaseSeverName()  
  68.     {  
  69.         this.Cursor = Cursors.WaitCursor;  
  70.           
  71.         cmbSeverName.Items.Clear();  
  72.         DataTable table = SmoApplication.EnumAvailableSqlServers();     //<----  
  73.         foreach (DataRow row in table.Rows)  
  74.         {  
  75.             string s = "";  
  76.             foreach (DataColumn c in table.Columns)  
  77.             {  
  78.                 s += c.Caption + ": " + row[c] + "/n";  
  79.                 if (c.Caption == "Server")  
  80.                 {  
  81.                     this.cmbSeverName.Items.Add(row[c]);  
  82.                 }  
  83.             }  
  84.   
  85.         }  
  86.         this.cmbSeverName.SelectedIndex = 0;  
  87.         this.Cursor = Cursors.Default;  
  88.     }  
  89.   
  90.     /// <summary>  
  91.     /// 获取服务器对应的数据库名称  
  92.     /// </summary>  
  93.     /// <param name="strServerName">服务器名称</param>  
  94.     /// <param name="strUserName">用户名</param>  
  95.     /// <param name="strPwd">密码</param>  
  96.     /// <param name="bIsWindowObscured">是否为Windows验证</param>  
  97.     /// <returns></returns>  
  98.     public string[] GetDataBaseList(string strServerName, string strUserName, string strPwd, bool bIsWindowObscured)  
  99.     {  
  100.          string[] aTables = null;  
  101.         
  102.         Server svr = new Server();  
  103.         svr.ConnectionContext.ServerInstance = strServerName;  
  104.         svr.ConnectionContext.LoginSecure = bIsWindowObscured;  
  105.         if (!bIsWindowObscured)  
  106.         {  
  107.             svr.ConnectionContext.Login = strUserName;  
  108.             svr.ConnectionContext.Password = strPwd;  
  109.         }  
  110.         try  
  111.         {  
  112.             svr.ConnectionContext.Connect();  
  113.             aTables = new string[svr.Databases.Count];  
  114.             int i = 0;  
  115.             foreach (Database db in svr.Databases)  
  116.             {  
  117.                 aTables[i] = db.Name;  
  118.                 i++;  
  119.             }  
  120.         }  
  121.         catch (System.Exception ex)  
  122.         {  
  123.             MessageBox.Show(ex.Message.ToString());  
  124.             //return   
  125.         }  
  126.   
  127.   
  128.         return aTables;  
  129.     }  
  130.   
  131.     private void cmbSeverName_Click(object sender, EventArgs e)  
  132.     {  
  133.     }  
  134.   
  135.     private void frmDataBaseConnect_Load(object sender, EventArgs e)  
  136.     {  
  137.         SetEnable(false);  
  138.         cmbDataBase.Enabled = false;  
  139.     }  
  140.   
  141.     private void txtUserName_Validated(object sender, EventArgs e)  
  142.     {  
  143.           
  144.     }  
  145.   
  146.     /// <summary>  
  147.     /// 用户名不为空时允许“确定”按钮弹出  
  148.     /// </summary>  
  149.     private void txtUserNameChange()  
  150.     {  
  151.         if (txtUserName.Text.Trim().Length != 0 || rbWCheck.Checked)  
  152.         {  
  153.             btnOK.Enabled = true;  
  154.         }  
  155.         else  
  156.         {  
  157.             btnOK.Enabled = false;  
  158.         }  
  159.     }  
  160.   
  161.     private void txtUserName_TextChanged(object sender, EventArgs e)  
  162.     {  
  163.         txtUserNameChange();  
  164.     }  
  165.   
  166.     private void cmbSeverName_DropDown(object sender, EventArgs e)  
  167.     {  
  168.         if (cmbSeverName.Items.Count==0)  
  169.         {  
  170.             btnReGetServerName.Enabled = false;  
  171.             GetDataBaseSeverName();  
  172.             btnReGetServerName.Enabled = true;  
  173.         }  
  174.   
  175.         if (cmbSeverName.Items.Count!=0)  
  176.         {  
  177.             cmbDataBase.Enabled = true;  
  178.         }  
  179.     }  
  180.   
  181.     /// <summary>  
  182.     /// 数据库名称下拉框点击时间,获取数据库列表  
  183.     /// </summary>  
  184.     /// <param name="sender"></param>  
  185.     /// <param name="e"></param>  
  186.     private void cmbDataBase_DropDown(object sender, EventArgs e)  
  187.     {  
  188.         this.Cursor = Cursors.WaitCursor;  
  189.   
  190.         cmbDataBase.Items.Clear();  
  191.   
  192.         if (cmbSeverName.Text.Trim().Length != 0)  
  193.         {  
  194.   
  195.             try  
  196.             {  
  197.                 string[] aDataBaseNames = GetDataBaseList(cmbSeverName.Text.Trim().ToString(), txtUserName.Text.Trim().ToString(), txtUserName.Text.Trim().ToString(),rbWCheck.Checked);  
  198.   
  199.                 if (aDataBaseNames != null)  
  200.                 {  
  201.   
  202.                     for (int i = 0; i < aDataBaseNames.Length; i++)  
  203.                     {  
  204.                         cmbDataBase.Items.Add(aDataBaseNames[i]);  
  205.                     }  
  206.                     if (cmbDataBase.Items.Count != 0)  
  207.                     {  
  208.                         cmbDataBase.SelectedIndex = 0;  
  209.                     }  
  210.                 }  
  211.             }  
  212.             catch (System.Exception ex)  
  213.             {  
  214.                 MessageBox.Show(ex.Message.ToString(), "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  215.             }  
  216.   
  217.         }  
  218.         this.Cursor = Cursors.Default;  
  219.   
  220.     }  
  221.   
  222.     private void btnCancel_Click(object sender, EventArgs e)  
  223.     {  
  224.         this.Close();  
  225.     }  
  226.   
  227.     private void btnTestConnect_Click(object sender, EventArgs e)  
  228.     {  
  229.         if (CheckConnect())  
  230.        {  
  231.            MessageBox.Show("连接成功!""连接成功", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  232.   
  233.        }  
  234.         else  
  235.         {  
  236.             MessageBox.Show("连接不成功!""连接不成功", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  237.   
  238.         }  
  239.     }  
  240.   
  241.     private void cmbSeverName_SelectedValueChanged(object sender, EventArgs e)  
  242.     {  
  243.     }  
  244.   
  245.     private void btnOK_Click(object sender, EventArgs e)  
  246.     {  
  247.         if (!CheckConnect())  
  248.         {  
  249.             DialogResult d = MessageBox.Show("连接不成功,是否继续配置?""连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);  
  250.             if (d == DialogResult.No)  
  251.             {  
  252.                 _strConnetionString = "";  
  253.                 this.Close();  
  254.             }  
  255.         }  
  256.         else  
  257.         {  
  258.             if (rbWCheck.Checked)  
  259.             {  
  260.                 //拼写Windows验证的链接字符串  
  261.                 _strConnetionString = strDataSource + cmbSeverName.Text.Trim().ToString() + strDataBaseName + cmbDataBase.Text.Trim().ToString() + ";Integrated Security=True";  
  262.             }  
  263.             else  
  264.             {  
  265.                 //拼写SQL验证的连接字符串  
  266.                 _strConnetionString = strDataSource + cmbSeverName.Text.Trim().ToString()  
  267.                     + strDataBaseName + cmbDataBase.Text.Trim().ToString() + strCheckType + strUserName  
  268.                     + txtUserName.Text.Trim().ToString() + strPwd + txtPwd.Text.Trim().ToString();  
  269.             }  
  270.             this.Close();  
  271.         }  
  272.     }  
  273.   
  274.     private bool CheckConnect()  
  275.     {  
  276.         bool bResult = false;  
  277.         //确定按钮点击后,检查是否成功连接,如果成连接,置标志位True,否则为false  
  278.         //false时,在获取程序连接字符串时提示没有配置成功  
  279.   
  280.         string strConnectString;// = ConnetionString;  
  281.   
  282.         if (rbWCheck.Checked)  
  283.         {  
  284.             //拼写Windows验证的链接字符串  
  285.             strConnectString = strDataSource + cmbSeverName.Text.Trim().ToString() + strDataBaseName + cmbDataBase.Text.Trim().ToString() + ";Integrated Security=True";  
  286.         }  
  287.         else  
  288.         {  
  289.             //拼写SQL验证的连接字符串  
  290.             strConnectString = strDataSource + cmbSeverName.Text.Trim().ToString()  
  291.                 + strDataBaseName + cmbDataBase.Text.Trim().ToString() + strCheckType + strUserName  
  292.                 + txtUserName.Text.Trim().ToString() + strPwd + txtPwd.Text.Trim().ToString();  
  293.         }  
  294.         SqlConnection myConnection = new SqlConnection(strConnectString);  
  295.         try  
  296.         {  
  297.             myConnection.Open();  
  298.             //MessageBox.Show("测试连接成功!", "测试连接", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  299.             bResult = true;  
  300.         }  
  301.         catch //(System.Exception ex)  
  302.         {  
  303.             //MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);  
  304.             bResult = false;  
  305.         }  
  306.         finally  
  307.         {  
  308.             myConnection.Close();  
  309.         }  
  310.   
  311.         return bResult;  
  312.     }  
  313. }  

[c-sharp] view plaincopy
  1. public class classDataBaseOption  
  2. {  
  3.     private INIClass iniCls = new INIClass(AppDomain.CurrentDomain.BaseDirectory + "//DataBaseConn.ini");  
  4.     private System.ComponentModel.BackgroundWorker bwShow = new System.ComponentModel.BackgroundWorker();  
  5.   
  6.     /// <summary>  
  7.     /// 是否连接正确  
  8.     /// </summary>  
  9.     private bool _bConnected = false;  
  10.     public bool Connected  
  11.     {  
  12.         get { return _bConnected; }  
  13.         //set{——}  
  14.     }  
  15.   
  16.     /// <summary>  
  17.     /// 连接字符串  
  18.     /// </summary>  
  19.     private string _ConnectionString;  
  20.     public string ConnectionString  
  21.     {  
  22.         get { return _ConnectionString; }  
  23.         set { _ConnectionString = value; }  
  24.     }  
  25.   
  26.     public classDataBaseOption()  
  27.     {  
  28.         GetConnectionString();  
  29.     }  
  30.     public classDataBaseOption(bool bResetConnectionString)  
  31.     {  
  32.         if (bResetConnectionString)  
  33.         {  
  34.             ResetConnectString();  
  35.         }  
  36.         //GetConnectionString();  
  37.     }  
  38.   
  39.       
  40.     /// <summary>  
  41.     /// 重新设置链接字符串  
  42.     /// </summary>  
  43.     private void ResetConnectString()  
  44.     {  
  45.         frmDataBaseConnect frmConnect = new frmDataBaseConnect();  
  46.         frmConnect.ShowDialog();  
  47.   
  48.         //_bConnected = frmConnect.  
  49.         if (frmConnect.ConnetionString == "")  
  50.         {  
  51.             _bConnected = false;  
  52.         }  
  53.         else  
  54.         {  
  55.             //存储新的链接字符串  
  56.             iniCls.IniWriteValue("ConnectionString""SqlConnectionString", frmConnect.ConnetionString);  
  57.             _ConnectionString = frmConnect.ConnetionString;  
  58.             _bConnected = true;  
  59.         }  
  60.     }  
  61.   
  62.   
  63.     //获取连接字符串  
  64.     private void GetConnectionString()  
  65.     {  
  66.         string strConnectionString = iniCls.IniReadValue("ConnectionString""SqlConnectionString");  
  67.   
  68.         if (CheckConnect(strConnectionString))  
  69.         {  
  70.             _bConnected = true;  
  71.             _ConnectionString = strConnectionString;  
  72.         }  
  73.         else  
  74.         {  
  75.             ResetConnectString();  
  76.             //_bConnected = true;  
  77.         }  
  78.     }  
  79.   
  80.     /// <summary>  
  81.     /// 根据链接字符串测试可连接性  
  82.     /// </summary>  
  83.     /// <param name="strConnectionString">连接字符串</param>  
  84.     /// <returns></returns>  
  85.     private bool CheckConnect(string strConnectionString)  
  86.     {  
  87.         bool bResult = false;  
  88.         SqlConnection myConnection = new SqlConnection(strConnectionString);  
  89.         try  
  90.         {  
  91.             myConnection.Open();  
  92.             //MessageBox.Show("测试连接成功!", "测试连接", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  93.             bResult = true;  
  94.         }  
  95.         catch //(System.Exception ex)  
  96.         {  
  97.             //MessageBox.Show("连接不成功,是否继续配置?", "连接不成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);  
  98.             bResult = false;  
  99.         }  
  100.         finally  
  101.         {  
  102.             myConnection.Close();  
  103.         }  
  104.   
  105.         return bResult;  
  106.     }  
  107. }  
  108.   
  109. /// <summary>  
  110. /// 读取ini文件  
  111. /// </summary>  
  112. public class INIClass  
  113. {  
  114.     public string inipath;  
  115.     [DllImport("kernel32")]  
  116.     private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);  
  117.     [DllImport("kernel32")]  
  118.     private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);  
  119.     /// <summary>  
  120.     /// 构造方法  
  121.     /// </summary>  
  122.     /// <param name="INIPath">文件路径</param>  
  123.     public INIClass(string INIPath)  
  124.     {  
  125.         inipath = INIPath;  
  126.     }  
  127.     /// <summary>  
  128.     /// 写入INI文件  
  129.     /// </summary>  
  130.     /// <param name="Section">项目名称(如 [TypeName] )</param>  
  131.     /// <param name="Key">键</param>  
  132.     /// <param name="Value">值</param>  
  133.     public void IniWriteValue(string Section, string Key, string Value)  
  134.     {  
  135.         WritePrivateProfileString(Section, Key, Value, this.inipath);  
  136.     }  
  137.     /// <summary>  
  138.     /// 读出INI文件  
  139.     /// </summary>  
  140.     /// <param name="Section">项目名称(如 [TypeName] )</param>  
  141.     /// <param name="Key">键</param>  
  142.     public string IniReadValue(string Section, string Key)  
  143.     {  
  144.         StringBuilder temp = new StringBuilder(500);  
  145.         int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);  
  146.         return temp.ToString();  
  147.     }  
  148.     /// <summary>  
  149.     /// 验证文件是否存在  
  150.     /// </summary>  
  151.     /// <returns>布尔值</returns>  
  152.     public bool ExistINIFile()  
  153.     {  
  154.         return File.Exists(inipath);  
  155.     }  
  156. }  

 

[c-sharp] view plaincopy
  1. //调用代码              
  2. classDataBaseOption clsCheckConnection = new classDataBaseOption();  
  3.             if (clsCheckConnection.Connected)  
  4.             {  
  5.                 clsConn.GetConnectionString = clsCheckConnection.ConnectionString;  
  6.             }