我在數據庫裡有一個 users 的表,如下:
ID UserName UserPasswd
1 admin admin
2 user user
3 guest guest
我准備這樣做,先判斷輸入的用戶名是否和表裡的UserName相同,如果相同,再比較相同UserName下的UserPasswd,如果這些都正確了,就可以進入系統了。
全部代碼如下(我把我寫的部分用黑體):
Form1的代碼:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClIEnt;
namespace login
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtUser;
private System.Windows.Forms.TextBox txtPasswd;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
/// <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 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtUser = new System.Windows.Forms.TextBox();
this.txtPasswd = new System.Windows.Forms.TextBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(40, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "用戶名:";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(40, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "密碼:";
//
// txtUser
//
this.txtUser.Location = new System.Drawing.Point(152, 24);
this.txtUser.Name = "txtUser";
this.txtUser.TabIndex = 2;
this.txtUser.Text = "";
//
// txtPasswd
//
this.txtPasswd.Location = new System.Drawing.Point(152, 72);
this.txtPasswd.Name = "txtPasswd";
this.txtPasswd.PassWordChar = '*';
this.txtPasswd.TabIndex = 3;
this.txtPasswd.Text = "";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(40, 120);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 23);
this.btnOK.TabIndex = 4;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(176, 120);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// Form1
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnCancel;
this.ClIEntSize = new System.Drawing.Size(298, 167);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.txtPasswd);
this.Controls.Add(this.txtUser);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(304, 192);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(304, 192);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Login";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.SetDesktopLocation(280,180);
}
private void btnOK_Click(object sender, System.EventArgs e)
{
Form2 theOwner = (Form2)this.Owner;
if(this.txtUser.Text == "")
{
MessageBox.Show("用戶名不能為空!","錯誤");
}
else if(this.txtPasswd.Text == "")
{
MessageBox.Show("密碼不能為空!","錯誤");
}
else
{
if(IsUser(this.txtUser.Text))
{
if(this.txtPasswd.Text == LoginUser(this.txtUser.Text))
{
this.DialogResult = DialogResult.OK;
theOwner.getUserName = this.txtUser.Text;
}
else
{
MessageBox.Show("用戶名或密碼錯誤!");
}
}
else
{
MessageBox.Show("用戶名或密碼錯誤!");
}
}
}
private string LoginUser(string User)
{
SqlConnection conn = new SqlConnection("XXXXXXXX");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Login";
cmd.Connection = conn;
conn.Open();
SqlParameter parName = new SqlParameter("@Name",SqlDbType.VarChar,50);
parName.Value = User;
cmd.Parameters.Add(parName);
cmd.ExecuteNonQuery();
conn.Close();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0].Rows[0]["UserPasswd"].ToString();
}
private bool IsUser(string User)
{
SqlConnection conn = new SqlConnection("XXXXXXXX");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "IsUser";
cmd.Connection = conn;
conn.Open();
SqlParameter parName = new SqlParameter("@UserName",SqlDbType.VarChar,50);
parName.Value = User;
cmd.Parameters.Add(parName);
cmd.ExecuteNonQuery();
conn.Close();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
int n;
n = ds.Tables[0].Rows.Count;
if(n > 0)
return true;
else
return false;
}
}
}
在Form2中我設計了一個label ,讓它接受從Form1傳過來的UserName,這樣我們在以後的設計中好判斷用戶的權限的大小。
Form2的代碼:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClIEnt;
namespace login
{
/// <summary>
/// Form2 的摘要說明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private string UserName;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
Form1 myForm = new Form1();
myForm.ShowDialog(this);
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(128, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(152, 40);
this.label1.TabIndex = 0;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClIEntSize = new System.Drawing.Size(520, 357);
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form2());
}
private void Form2_Load(object sender, System.EventArgs e)
{
this.SetDesktopLocation(200,180);
this.label1.Text = UserName;
}
public string getUserName
{
get
{
return UserName;
}
set
{
UserName = value;
}
}
}
}