一个简单的登陆功能模块
需求分析
*需要建立与数据库的连接
*需要能输入登陆信息的页面
*如果成功登陆,则显示成功页面
数据库设计
用sql server2000作为后台支持的数据库。设计步骤如下:
*新建一个名为UserData的数据库。
*在UserData数据库中新建一张名为Users的表,用来记录用户信息。
创建Users表的sql语句如下:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE[dbo].[Users](
[UserID][int][DENTITY(1,1)NOT NULL,
[UserName][nvarchar](50)COLLATE Chinese_PRC_CI_AS NOTE NULL,
[Password][nvarchar](50)COLLATE Chinese_PRC_CI_AS NULL,
[Tel][nvarchar](50)COLLATE Chinese_PRC_CI_AS NULL,
[Mobile][nvarchar](50)COLLATE Chinese_PRC_CI_AS NULL,
[Fax][nvarchar](50)COLLATE Chinese_PRC_CI_AS NULL,
[Address][nvarchar](50)COLLATE Chinese_PRC_CI_AS NULL,
[ZipCode][nvarchar](50)COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT[PK_Users_1]PRIMARY KEY CLUSTERED
{
[UserID]ASC
}WITH(IGNORE_DUP_KEY=OFF)ON[PRIMARY]
)ON[PRIMARY]
上述语句已经将UserID设置成主键,为了更详细的说明,将设置主键的sql语句罗列如下,其中把UserID字段设置成Users表的主键:
CONSTRAINT PK_UserRole PRIMARY KEY CLUSTERED
{
UserID
}
GO
下面利用Visual Studio 2005,新建Windows应用程序,右击“解决方案资源管理器”,添加一个类,名为Conn.cs。用来设置数据库的连接属性。这里没有用配置文件,而是用这个类文件实现的。这样的封装方式可以部分的实现业务逻辑与数据库访问逻辑的分离,向数据库业务的使用者屏蔽具体的数据库连接和操作细节。
Conn.cs中,封装了本模块需要的连接数据库的通用方法,其代码如下所示:
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace Windows登陆验证
{
class Conn
{
private string IP;
private string DbName;
private string DbUser;
private string Key;
public Conn(string ip, string dbName, string dbUser, string key)
{
IP = ip;
DbName = dbName;
DbUser = dbUser;
Key = key;
}
public SqlConnection creatConn()
{
SqlConnection thisConnection = new SqlConnection(@"Server=" + IP + ";user id=" + DbUser + ";initial catalog=" + DbName + ";password=" + Key);
thisConnection.Open();
return thisConnection;
}
}
}
这个模块封装了常用的ADO.NET组件连接访问数据库的通用方法。将数据库的连接SqlConnection创立在函数creatConn中,这种做法可以屏蔽数据库底层的实现细节,以后如果要修改连接和访问数据库的实现方式,就不会影响到业务逻辑中的数据库相关代码。
设计主窗体界面
将默认命名为Form1.cs的设计界面中,按照如下代码形成的界面设计:
Form1.Designer.cs的代码为:
{
partial class Form1
{
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(89, 12);
this.label1.TabIndex = 0;
this.label1.Text = "数据库服务器:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(144, 9);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(110, 21);
this.textBox1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(22, 43);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 2;
this.label2.Text = "数据库名:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(144, 43);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(110, 21);
this.textBox2.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(22, 82);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(89, 12);
this.label3.TabIndex = 4;
this.label3.Text = "数据库用户名:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(144, 82);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(110, 21);
this.textBox3.TabIndex = 5;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(22, 122);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(77, 12);
this.label4.TabIndex = 6;
this.label4.Text = "数据库密码:";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(144, 122);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(110, 21);
this.textBox4.TabIndex = 7;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(22, 175);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(53, 12);
this.label5.TabIndex = 8;
this.label5.Text = "用户名:";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(144, 175);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(110, 21);
this.textBox5.TabIndex = 9;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(22, 210);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(41, 12);
this.label6.TabIndex = 10;
this.label6.Text = "密码:";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(144, 210);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(110, 21);
this.textBox6.TabIndex = 11;
//
// button1
//
this.button1.Location = new System.Drawing.Point(37, 255);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 12;
this.button1.Text = "登陆";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// button2
//
this.button2.Location = new System.Drawing.Point(159, 255);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 13;
this.button2.Text = "关闭";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 299);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.label6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "登陆功能模块";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
编写业务逻辑:
编写Form1.cs的业务逻辑设计代码的操作步骤如下。
1、双击“登陆”按钮,进入“登陆”按钮的Click时间处理函数开发界面。该处理函数定义如下:
{
Conn con = new Conn(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, this.textBox4.Text);
SqlConnection sqlConnection = con.creatConn();
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName='" + this.textBox5.Text + "' and Password='" + this.textBox6.Text + "'", sqlConnection);
string t = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (t == "1")
{
this.Visible = false;
Login login = new Login();
login.ShowDialog();
}
else
{
MessageBox.Show("登录失败", "提醒");
}
}
通过单击“登陆”按钮,实现登陆验证功能。首先获取用户输入的数据库信息和登陆信息。然后通过Conn.cs中的,creatConn函数创建数据库连接,接着建立数据库操作命令,这里用了简单的sql查询语句。最后执行数据库命令,使用的是ExecuteScalar()命令。如果返回1,表示用户的登陆信息是正确的,可以通过验证,否则提示错误信息。
2、双击“关闭”按钮,进入“关闭”按钮的Click时间处理函数开发界面。该处理函数定义如下:
{
this.Close();
}
该函数实现了窗体的关闭功能。
之后设计登陆成功页面,如图所示:
Login.Designer.cs的代码为:
{
partial class Login
{
/// <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.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(109, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 0;
this.label1.Text = "登陆成功!";
//
// Login
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Name = "Login";
this.Text = "login";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
登陆成功后,向用户显示“登陆成功!”
注:新人一定要做的例子,若有问题请留言回复,另欢迎大家批评指正。