登录安全与连接池的应用

来源:互联网 发布:淘宝主图尺寸怎么修改 编辑:程序博客网 时间:2024/06/14 15:35

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Demo1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Button btnTest;
  private System.Windows.Forms.Label lbUse;
  private System.Windows.Forms.Label lbUnUse;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox tbUserName;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.TextBox tbPassWord;
  private System.Windows.Forms.Button btnLogin;
  private System.Windows.Forms.Button btnSecurity;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.lbUnUse = new System.Windows.Forms.Label();
   this.lbUse = new System.Windows.Forms.Label();
   this.btnTest = new System.Windows.Forms.Button();
   this.label2 = new System.Windows.Forms.Label();
   this.label1 = new System.Windows.Forms.Label();
   this.groupBox2 = new System.Windows.Forms.GroupBox();
   this.btnSecurity = new System.Windows.Forms.Button();
   this.btnLogin = new System.Windows.Forms.Button();
   this.tbPassWord = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.tbUserName = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.groupBox1.SuspendLayout();
   this.groupBox2.SuspendLayout();
   this.SuspendLayout();
   //
   // groupBox1
   //
   this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                     this.lbUnUse,
                     this.lbUse,
                     this.btnTest,
                     this.label2,
                     this.label1});
   this.groupBox1.Location = new System.Drawing.Point(24, 16);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(336, 152);
   this.groupBox1.TabIndex = 0;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "POOl";
   //
   // lbUnUse
   //
   this.lbUnUse.Location = new System.Drawing.Point(216, 72);
   this.lbUnUse.Name = "lbUnUse";
   this.lbUnUse.Size = new System.Drawing.Size(72, 23);
   this.lbUnUse.TabIndex = 4;
   this.lbUnUse.Text = "0";
   //
   // lbUse
   //
   this.lbUse.Location = new System.Drawing.Point(32, 72);
   this.lbUse.Name = "lbUse";
   this.lbUse.Size = new System.Drawing.Size(72, 23);
   this.lbUse.TabIndex = 3;
   this.lbUse.Text = "0";
   //
   // btnTest
   //
   this.btnTest.Location = new System.Drawing.Point(120, 120);
   this.btnTest.Name = "btnTest";
   this.btnTest.TabIndex = 2;
   this.btnTest.Text = "Test";
   this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(208, 32);
   this.label2.Name = "label2";
   this.label2.TabIndex = 1;
   this.label2.Text = "UnUse Pool";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(24, 32);
   this.label1.Name = "label1";
   this.label1.TabIndex = 0;
   this.label1.Text = "Use Pool";
   //
   // groupBox2
   //
   this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
                     this.btnSecurity,
                     this.btnLogin,
                     this.tbPassWord,
                     this.label4,
                     this.tbUserName,
                     this.label3});
   this.groupBox2.Location = new System.Drawing.Point(24, 184);
   this.groupBox2.Name = "groupBox2";
   this.groupBox2.Size = new System.Drawing.Size(336, 168);
   this.groupBox2.TabIndex = 1;
   this.groupBox2.TabStop = false;
   this.groupBox2.Text = "Security";
   //
   // btnSecurity
   //
   this.btnSecurity.Location = new System.Drawing.Point(176, 120);
   this.btnSecurity.Name = "btnSecurity";
   this.btnSecurity.Size = new System.Drawing.Size(112, 23);
   this.btnSecurity.TabIndex = 5;
   this.btnSecurity.Text = "SecurityLogin";
   this.btnSecurity.Click += new System.EventHandler(this.btnSecurity_Click);
   //
   // btnLogin
   //
   this.btnLogin.Location = new System.Drawing.Point(24, 120);
   this.btnLogin.Name = "btnLogin";
   this.btnLogin.Size = new System.Drawing.Size(104, 23);
   this.btnLogin.TabIndex = 4;
   this.btnLogin.Text = "Login";
   this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
   //
   // tbPassWord
   //
   this.tbPassWord.Location = new System.Drawing.Point(176, 80);
   this.tbPassWord.Name = "tbPassWord";
   this.tbPassWord.TabIndex = 3;
   this.tbPassWord.Text = "";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(32, 88);
   this.label4.Name = "label4";
   this.label4.TabIndex = 2;
   this.label4.Text = "PassWord";
   //
   // tbUserName
   //
   this.tbUserName.Location = new System.Drawing.Point(176, 40);
   this.tbUserName.Name = "tbUserName";
   this.tbUserName.TabIndex = 1;
   this.tbUserName.Text = "";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(32, 40);
   this.label3.Name = "label3";
   this.label3.TabIndex = 0;
   this.label3.Text = "UserName";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(384, 365);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.groupBox2,
                    this.groupBox1});
   this.Name = "Form1";
   this.Text = "ADO.NET连接示例";
   this.groupBox1.ResumeLayout(false);
   this.groupBox2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }


  private void btnTest_Click(object sender, System.EventArgs e)
  {
   string strConUnusePool = "Server=localhost; Integrated Security=SSPI; database=mydatabase;pooling=false";
   string strConusePool = "Server=localhost; Integrated Security=SSPI; database=mydatabase;"
    +"pooling=true;connection lifetime=5";
   int nConNum = 50;
   //计算不使用连接池创建连接的时间
   DateTime dtStart = DateTime.Now;
   for(int i=1;i<=nConNum;i++)
   {
    using(SqlConnection con = new SqlConnection(strConUnusePool))
    {
     con.Open();
     con.Close();
    }
   }
   DateTime dtEnd = DateTime.Now;
   TimeSpan ts = dtEnd-dtStart;
   lbUnUse.Text = ts.Milliseconds.ToString();
   //计算使用连接池的时间
   dtStart = DateTime.Now;
   for(int i=1;i<=nConNum;i++)
   {
    using(SqlConnection con = new SqlConnection(strConusePool))
    {
     con.Open();
     con.Close();
    }
   }
   dtEnd = DateTime.Now;
   ts = dtEnd-dtStart;
   lbUse.Text = ts.Milliseconds.ToString();


  }

  private void btnLogin_Click(object sender, System.EventArgs e)
  {
   string strCon = "Server=localhost; Integrated Security=SSPI; database=mydatabase;pooling=false";
   SqlConnection con = new SqlConnection(strCon);
   string strSql = "select * from ScoreTable where UserName='"+tbUserName.Text+"' and PassWord= '"+tbPassWord.Text+"'";
   SqlCommand com = new SqlCommand(strSql,con);
   con.Open();
   SqlDataReader sdr = com.ExecuteReader();
   if(sdr.Read())
     MessageBox.Show("Authenticated");
   else
     MessageBox.Show("Invalid User");
   sdr.Close();
   con.Close();

  }

  private void btnSecurity_Click(object sender, System.EventArgs e)
  {
   string strCon = "server=(local);database=mydatabase;uid=sa;pwd=111;pooling=false";
   SqlConnection con = new SqlConnection(strCon);
   string strSql = "select * from ScoreTable where UserName='"+tbUserName.Text.Replace("'", "''")
    +"' and PassWord= '"+tbPassWord.Text.Replace("'", "''")+"'";
   SqlCommand com = new SqlCommand(strSql,con);
   con.Open();
   SqlDataReader sdr = com.ExecuteReader();
   if(sdr.Read())
    MessageBox.Show("Authenticated");
   else
    MessageBox.Show("Invalid User");
   sdr.Close();
   con.Close();
  }
 }
}
 

原创粉丝点击