电子商务之数据存储流程(五)
2009-11-23 08:53
197 查看
首先我想还是给大家过一遍数据存储的流程吧。选个最简单的例子,用户登录,将用户从数据库中取出来。
首先需要在SQL数据库中编写存储过程,选存储过程+传递参数用SqlParameter是因为,除非是ADO.NET有漏洞,那么就绝对不会发生SQL注入。据我所知SQL注入发生在借用用户输入拼接生成SQL语句的地方。其次执行数据操作的性能也提高了。
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 Shop.Common;
using Shop.BusinessLogic;
public partial class Login : BasePage
{
protected void Page_Load( object sender , EventArgs e )
{
textUsername.Focus();
}
protected void commandLogin_Click( object sender , EventArgs e )
{
if ( IsValid )
{
EndUserEntity enduser = new EndUserEntity();
ProcessEndUserLogin processlogin = new ProcessEndUserLogin();
enduser.UserContactInformation.Email = textUsername.Text;
enduser.Password = textPassword.Text;
processlogin.EndUser = enduser;
try
{
processlogin.Invoke();
}
catch
{
Response.Redirect( "ErrorPage.aspx" );
}
if ( processlogin.IsAuthenticated )
{
Response.Cookies["Authenticated"].Value = "True";
base.CurrentEndUser = processlogin.EndUser;
if ( Request.Cookies["ReturnURL"] != null )
{
Response.Redirect( Request.Cookies["ReturnURL"].Value );
}
else
{
Response.Redirect("Account/CustomerOrders.aspx");
}
}
else
{
labelMessage.Text = "Invalid login!";
}
}
}
}
这里我想要说明一点就是try...catch的用法,用得非常好,如果 processlogin.Invoke();就是我们刚才写的那么多代码里发生的错误或异常在这里就可以轻松捕抓到并向用户提示页面出错,我个人以前在这方面会忽略,所以认为这里很经典
,注意到这里用户的密码没有加密,我们也可自己调用System.Web.Security.FormsAuthencation.HashPasswordForStoringInConfigFile()进行加密。下面的一些代码会涉及到很多内容我会在下面的内容和大家一起分析(注意到页面继承的BasePage类,而不是我们经常看到System.Web.UI.Page类)另外要强调的是,要注意引用这些程序集,例如我们在Shop.DataAccess添加引用项目Shop.Common类库,Shop.BusinessLogic添加引用Shop.DataAccess类库以及Shop.Common类库等等相信大家这个都会吧 -_-
首先需要在SQL数据库中编写存储过程,选存储过程+传递参数用SqlParameter是因为,除非是ADO.NET有漏洞,那么就绝对不会发生SQL注入。据我所知SQL注入发生在借用用户输入拼接生成SQL语句的地方。其次执行数据操作的性能也提高了。
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 Shop.Common;
using Shop.BusinessLogic;
public partial class Login : BasePage
{
protected void Page_Load( object sender , EventArgs e )
{
textUsername.Focus();
}
protected void commandLogin_Click( object sender , EventArgs e )
{
if ( IsValid )
{
EndUserEntity enduser = new EndUserEntity();
ProcessEndUserLogin processlogin = new ProcessEndUserLogin();
enduser.UserContactInformation.Email = textUsername.Text;
enduser.Password = textPassword.Text;
processlogin.EndUser = enduser;
try
{
processlogin.Invoke();
}
catch
{
Response.Redirect( "ErrorPage.aspx" );
}
if ( processlogin.IsAuthenticated )
{
Response.Cookies["Authenticated"].Value = "True";
base.CurrentEndUser = processlogin.EndUser;
if ( Request.Cookies["ReturnURL"] != null )
{
Response.Redirect( Request.Cookies["ReturnURL"].Value );
}
else
{
Response.Redirect("Account/CustomerOrders.aspx");
}
}
else
{
labelMessage.Text = "Invalid login!";
}
}
}
}
这里我想要说明一点就是try...catch的用法,用得非常好,如果 processlogin.Invoke();就是我们刚才写的那么多代码里发生的错误或异常在这里就可以轻松捕抓到并向用户提示页面出错,我个人以前在这方面会忽略,所以认为这里很经典
,注意到这里用户的密码没有加密,我们也可自己调用System.Web.Security.FormsAuthencation.HashPasswordForStoringInConfigFile()进行加密。下面的一些代码会涉及到很多内容我会在下面的内容和大家一起分析(注意到页面继承的BasePage类,而不是我们经常看到System.Web.UI.Page类)另外要强调的是,要注意引用这些程序集,例如我们在Shop.DataAccess添加引用项目Shop.Common类库,Shop.BusinessLogic添加引用Shop.DataAccess类库以及Shop.Common类库等等相信大家这个都会吧 -_-
相关文章推荐
- 电子商务之数据存储流程(五)
- HDFS的存储结构以及写入、读取hdfs数据操作流程简单总结
- gnocchi-采样数据存储流程分析(002)--数据的异步统计
- Atitit 数据分析存储位置的查找方法与流程attilax总结
- Hadoop HBase概念学习系列之HBase里的存储数据流程(二十三)
- ceph存储数据的详细流程(CRUSH)
- Android_数据存储的写法流程
- 创建存储流程引擎模板数据表
- 现在电子商务网站建设中数据库如何设计使数据存储量变大
- gnocchi-采样数据存储流程分析(001)--数据存储
- HBase数据检索流程与数据存储
- struts2开发流程及配置,域对象对数据存储的3种方式
- 工作流-关于业务数据和流程数据(上)
- python概述、数据运算及流程控制
- ios开发系列之文件的写入读取,NSUserDefaults存储数据,NSFileManager操作文件和文件夹
- T-SQL 存储过程: (修订版) 根据基本表结构及其数据生成 INSERT INTO ... 的 SQL
- 简单的存储过程实例2--对比三个表中的数据,将三个表中金额挑选一个最优值
- float数据在内存中是怎么存储的 AND IEEE754测试程序
- swift 快速奔跑的兔几 本节的内容是:数据存储NSUserDefaults
- 微服务架构下的分布式数据存储-技术之家