这是我第一次写详细的教程,为什么我要写这个教程?我知道,自学的路很痛苦,为了让以后的新同学能减少这样的痛苦,少走弯路,遂决定自己也写一写教程,一来是对自己的知识的一个总结,二来也是练练自己的写作,呵呵,开个玩笑:)
Asp.net入门-网络采购系统(1)
在这一部分的教程中,讲从sqlserver2000开始建一个LoginTable开始讲起,本节教程将包括:
1.数据表的创建
2.登录界面的制作
3.如何登录
4.如何区别用户
5.登录了该干什么
这样的5个问题。
1.数据表的创建:
大家都知道,我们所有的登录信息都是存放在数据库中的,所以,要想成功的登录到系统,首先得有一个数据库,或数据表来记录我们的登录信息,我喜欢把这个名字取为LoginTable,这完全可以按照同学自己的兴趣爱好取,你可以叫他AAA,BBB,呵呵。
首先需要一个ID,用于以后从数据库获取并修改用户使用,UserName用户名,Password密码,LeiBie用户类别,建表相关SQL语句如下:
CREATE
TABLE
[
LoginTable
]
(
[ ID ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ UserName ] [ varchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ Password ] [ varchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LeiBie ] [ int ] NULL ,
CONSTRAINT [ PK_LoginTable ] PRIMARY KEY CLUSTERED
(
[ ID ]
) ON [ PRIMARY ]
) ON [ PRIMARY ]
GO
直接把上面的代码复制到“查询分析器”中,运行一下就好了,当然,前面要加上一句use[目标数据库名]。
[ ID ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ UserName ] [ varchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ Password ] [ varchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LeiBie ] [ int ] NULL ,
CONSTRAINT [ PK_LoginTable ] PRIMARY KEY CLUSTERED
(
[ ID ]
) ON [ PRIMARY ]
) ON [ PRIMARY ]
GO
2.登录界面的创建:
在这儿,我直接复制粘贴过来我在Dreamweaver里面做好了的一个登录界面过来,有点丑,但是还比较实用,呵呵!
页面代码:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 公司采购系统 </ title >
< link href ="Css.css" type ="text/css" rel ="stylesheet" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />< style type ="text/css" >
<!--
body {
background-color : #333366 ;
}
-->
</ style ></ head >
< body >
< form id ="form1" runat ="server" >
< div >
< p > </ p >
< p > </ p >
< p > </ p >
< p > </ p >
< table width ="600" border ="0" align ="center" cellpadding ="0" cellspacing ="0" background ="Image/bg.gif" >
< tr >
< td colspan ="2" >< img src ="Image/logo.gif" width ="600" height ="150" /></ td >
</ tr >
< tr >
< td style ="width: 304px" >
</ td >
< td width ="299" >
</ td >
</ tr >
< tr >
< td style ="width: 304px" >< div align ="right" style ="text-align: right" >< img src ="Image/UserNamePic.gif" width ="127" height ="29" /></ div ></ td >
< td width ="299" > < asp:TextBox ID ="UserName" runat ="server" ></ asp:TextBox >< span style ="color: #ff0000" > * </ span ></ td >
</ tr >
< tr >
< td style ="width: 304px; height: 29px" >< div align ="right" >< img src ="Image/PasswordPic.gif" width ="127" height ="29" /></ div ></ td >
< td style ="height: 29px" > < asp:TextBox ID ="Password" runat ="server" TextMode ="Password" Width ="148px" ></ asp:TextBox >< span
style ="color: #ff0000" > * </ span ></ td >
</ tr >
< tr >
< td style ="width: 304px; height: 16px" >
</ td >
< td style ="height: 16px" >
</ td >
</ tr >
< tr >
< td style ="width: 304px; text-align: right" > < asp:Button ID ="UserLoginBtn" runat ="server" CssClass ="ButtonCss" Text ="用户登录"
Width ="120px" OnClick ="UserLoginBtn_Click" /></ td >
< td > < asp:Button ID ="AdminLoginBtn" runat ="server" CssClass ="ButtonCss" Text ="管理登录"
Width ="120px" OnClick ="AdminLoginBtn_Click" /></ td >
</ tr >
< tr >
< td colspan ="2" style ="height: 15px; text-align: center" >
< span style ="color: #ff0033" > 无用户名与密码请联系管理员注册 </ span ></ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
这样,一个页面就制作完成了,当然,光有页面是不可以的,那样的话只是一个空架子了,没有实际的意义,在这里,我先不忙贴出CS代码来,先请同学往下看,在如何登录里面,我会讲讲该页面的CS代码的编写。
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 公司采购系统 </ title >
< link href ="Css.css" type ="text/css" rel ="stylesheet" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />< style type ="text/css" >
<!--
body {
background-color : #333366 ;
}
-->
</ style ></ head >
< body >
< form id ="form1" runat ="server" >
< div >
< p > </ p >
< p > </ p >
< p > </ p >
< p > </ p >
< table width ="600" border ="0" align ="center" cellpadding ="0" cellspacing ="0" background ="Image/bg.gif" >
< tr >
< td colspan ="2" >< img src ="Image/logo.gif" width ="600" height ="150" /></ td >
</ tr >
< tr >
< td style ="width: 304px" >
</ td >
< td width ="299" >
</ td >
</ tr >
< tr >
< td style ="width: 304px" >< div align ="right" style ="text-align: right" >< img src ="Image/UserNamePic.gif" width ="127" height ="29" /></ div ></ td >
< td width ="299" > < asp:TextBox ID ="UserName" runat ="server" ></ asp:TextBox >< span style ="color: #ff0000" > * </ span ></ td >
</ tr >
< tr >
< td style ="width: 304px; height: 29px" >< div align ="right" >< img src ="Image/PasswordPic.gif" width ="127" height ="29" /></ div ></ td >
< td style ="height: 29px" > < asp:TextBox ID ="Password" runat ="server" TextMode ="Password" Width ="148px" ></ asp:TextBox >< span
style ="color: #ff0000" > * </ span ></ td >
</ tr >
< tr >
< td style ="width: 304px; height: 16px" >
</ td >
< td style ="height: 16px" >
</ td >
</ tr >
< tr >
< td style ="width: 304px; text-align: right" > < asp:Button ID ="UserLoginBtn" runat ="server" CssClass ="ButtonCss" Text ="用户登录"
Width ="120px" OnClick ="UserLoginBtn_Click" /></ td >
< td > < asp:Button ID ="AdminLoginBtn" runat ="server" CssClass ="ButtonCss" Text ="管理登录"
Width ="120px" OnClick ="AdminLoginBtn_Click" /></ td >
</ tr >
< tr >
< td colspan ="2" style ="height: 15px; text-align: center" >
< span style ="color: #ff0033" > 无用户名与密码请联系管理员注册 </ span ></ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
3.如何登录
虽然这个步骤不复杂,但是比较烦琐,希望同学们耐心的往下做。
1)请同学在根目录下建立一个App_Code文件夹,里面建一个DengLu.cs类,用于写一些get,set方法,具体代码如下:
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlHelper;
/// <summary>
/// DengLu 的摘要说明
/// </summary>
public class DengLu
{
public DengLu()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private int iD = 0 ;
private string userName = string .Empty;
private string password = string .Empty;
private string leiBie = string .Empty;
private int userClass = 0 ;
public int ID
{
get { return this .ID; }
set { this .ID = value; }
}
public string UserName
{
get { return this .userName; }
set { this .userName = value; }
}
public string Password
{
get { return this .password; }
set { this .password = value; }
}
public string LeiBie
{
get { return this .leiBie; }
set { this .leiBie = value; }
}
public int UserClass
{
get { return this .userClass; }
set { this .userClass = value; }
}
}
然后在在App_Code目录下建立一个实体(Entity)文件夹,并在这里新建一个文件,名字叫DengLuDAO.cs的一个类,这个类主要目的是用于数据的提交,具体代码为:
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlHelper;
/// <summary>
/// DengLu 的摘要说明
/// </summary>
public class DengLu
{
public DengLu()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private int iD = 0 ;
private string userName = string .Empty;
private string password = string .Empty;
private string leiBie = string .Empty;
private int userClass = 0 ;
public int ID
{
get { return this .ID; }
set { this .ID = value; }
}
public string UserName
{
get { return this .userName; }
set { this .userName = value; }
}
public string Password
{
get { return this .password; }
set { this .password = value; }
}
public string LeiBie
{
get { return this .leiBie; }
set { this .leiBie = value; }
}
public int UserClass
{
get { return this .userClass; }
set { this .userClass = value; }
}
}
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlHelper;
/// <summary>
/// DengLuDao 的摘要说明
/// </summary>
public class DengLuDao
{
public DengLuDao()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static string UserName = " @UserName " ;
private static string Password = " @Password " ;
private static string userClass = " @LeiBie " ;
private static string SELECT_LoginTable = " Select * from LoginTable where UserName=@UserName and Password=@Password " ;
public static DengLu DengLuTable(DengLu TiJiao2)
{
DengLu userInfo = null ;
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter(UserName,SqlDbType.NVarChar, 50 ),
new SqlParameter(Password,SqlDbType.NVarChar, 50 )};
parms[ 0 ].Value = TiJiao2.UserName;
parms[ 1 ].Value = TiJiao2.Password;
// 获取连接字符串,插入数据到SQL数据库
SqlDataReader read = SqlHelper.SqlHelpDao.ExecuteReader(GetConnectionString.ConnectionString, CommandType.Text, SELECT_LoginTable, parms);
while (read.Read())
{
userInfo = new DengLu();
userInfo.UserName = read[ " UserName " ].ToString();
userInfo.Password = read[ " Password " ].ToString();
userInfo.UserClass = int .Parse(read[ " LeiBie " ].ToString());
}
return userInfo;
}
}
是不是到这儿就完了呢?
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlHelper;
/// <summary>
/// DengLuDao 的摘要说明
/// </summary>
public class DengLuDao
{
public DengLuDao()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static string UserName = " @UserName " ;
private static string Password = " @Password " ;
private static string userClass = " @LeiBie " ;
private static string SELECT_LoginTable = " Select * from LoginTable where UserName=@UserName and Password=@Password " ;
public static DengLu DengLuTable(DengLu TiJiao2)
{
DengLu userInfo = null ;
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter(UserName,SqlDbType.NVarChar, 50 ),
new SqlParameter(Password,SqlDbType.NVarChar, 50 )};
parms[ 0 ].Value = TiJiao2.UserName;
parms[ 1 ].Value = TiJiao2.Password;
// 获取连接字符串,插入数据到SQL数据库
SqlDataReader read = SqlHelper.SqlHelpDao.ExecuteReader(GetConnectionString.ConnectionString, CommandType.Text, SELECT_LoginTable, parms);
while (read.Read())
{
userInfo = new DengLu();
userInfo.UserName = read[ " UserName " ].ToString();
userInfo.Password = read[ " Password " ].ToString();
userInfo.UserClass = int .Parse(read[ " LeiBie " ].ToString());
}
return userInfo;
}
}
不是的,需要在有个GetConnectionString这个类,用于和SQL数据库的连接,就是说,你要告诉他,怎么怎么找到数据库,GetConnectionString这个类放到App_Code目录下就好了:
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlHelper;
/// <summary>
/// GetConnectionString 的摘要说明
/// </summary>
public class GetConnectionString
{
public GetConnectionString()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings[ " WebCaiGouDBConnectionString " ].ConnectionString;
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlHelper;
/// <summary>
/// GetConnectionString 的摘要说明
/// </summary>
public class GetConnectionString
{
public GetConnectionString()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings[ " WebCaiGouDBConnectionString " ].ConnectionString;
}
当然,还需要修改web.config里面的相关<ConnectionString></ConnectionString>部分的代码。
下一步我们该做什么呢?
下一步我们该对刚才制作的登录界面里的Button写Click事件了,先把代码贴出来,在解释一下:
protected
void
UserLoginBtn_Click(
object
sender, EventArgs e)
{
DengLu user = new DengLu();
user.UserName = UserName.Text.Trim();
user.Password = Password.Text.Trim();
DengLu userInfo = DengLuDao.DengLuTable(user);
if (userInfo != null )
{
Session.Add( " LeiBie " , userInfo.UserClass);
if (userInfo.UserClass == 1 )
{
Response.Write( " <script>alert('用户登录成功');window.open(\ " . / UserManage.aspx\ " );</script> " );
}
if (userInfo.UserClass == 2 )
{
Response.Write( " <script>alert('管理登录成功');window.open(\ " . / AdminManage.aspx\ " );</script> " );
}
}
else
{
Response.Write( " <script>alert('登录失败!')</script> " );
}
}
在这里可以看到,这个是一个UserLoginBtn的click事件,首先我们得实例化一下,然后获取两个TextBox的值,并调用DengLuDao.DengLuTable(user)这样的一个方法,来插入到数据库里面进行验证,我采用了一个UserClass来作为用户类别的区分,你可以设置1为管理员,2为用户之类的,这些也都是你自己随便设置的,登录以后页面的跳转就是后面的那句window.open了,呵呵,这是最简单的一种登录方式的设计,新同学可以按照这样做一下试试,希望看我教程的同学多动手多动手试试,复制粘贴是学不到东西的。
{
DengLu user = new DengLu();
user.UserName = UserName.Text.Trim();
user.Password = Password.Text.Trim();
DengLu userInfo = DengLuDao.DengLuTable(user);
if (userInfo != null )
{
Session.Add( " LeiBie " , userInfo.UserClass);
if (userInfo.UserClass == 1 )
{
Response.Write( " <script>alert('用户登录成功');window.open(\ " . / UserManage.aspx\ " );</script> " );
}
if (userInfo.UserClass == 2 )
{
Response.Write( " <script>alert('管理登录成功');window.open(\ " . / AdminManage.aspx\ " );</script> " );
}
}
else
{
Response.Write( " <script>alert('登录失败!')</script> " );
}
}
在下面的一节教程中,我将会对UserManage管理平台该干什么,如何读取数据库里的数据,如何添加数据,如何修改数据,添加用户,删除用户之类的问题小小的讲一下,希望新同学能学到有用的东西。
如果您对我的教程有什么不清楚的地方,可以在回复中告诉我,我会及时来给您解答的。