日期:2011-05-19  浏览次数:20899 次


我在csdn里搜索了很久,也没有找到符合我要求的login文档,我这次把自己的心得和自己做的成果拿出来和大家分享一下,希望对后来的人能有一些帮助。我初次做,可能代码写的不是很规范,思路也不是很清晰,但是它能达到我要的效果就行了'      希望哪位兄弟帮忙完善一下我的代码。

我在数据库里有一个   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.Draw