慧云科技 > 首页 > 文章详情

用三层架构模式开发网站与普通的模式开发的特点(一)

本文来源:网站建设-网站制作|网络营销专家慧云科技-

抢滩登陆

2009-7-29 16:14:45

    这一篇主要介绍用普通方法来做的一个网站建设小项目。
    我先说一下我所做的一个模仿银行转账的小小项目:首先创建了一张表,只有三个字段CardID(帐号)CardPass(帐号密码)Moneys(余额)。
    一共有五个页面,一个是登陆页面(Login.aspx)一个首页(Default.aspx)一个余额查询页面(CheckMoney.aspx)一个转账页面(PostMoney.aspx)一个母板页(MasterPage.master)。
首先网站建设先说一下具体的流程:第一是必须先登陆,登陆后把帐号存在Session中,登陆后跳转到首页(Default.aspx)中,网页设计在首页中有两个超链接一个是跳转到余额查询页面,一个是跳转到转账页面,比较麻烦的就是转账时都需要做什么事情,专业研究建站研究第一就是判断要转账的帐号是否存在,第二是查找自己的余额,然后和要转账的钱进行比较,如果要转账的数目小于自己的余额,然后更新数据库中自己的余额同时更新目标帐号的余额,否则提示余额不足。然后再判断所要转账的帐号是否和自己的帐号相同,如果相同的话,提示不能给自己给自己转账.然后根据Session中传过来的值点击余额查询的页面,查询对应Session中传过来帐号的余额。
    首先在登陆页面的按钮中写
protected void Bton_Click(object sender, EventArgs e)
{
string sql = "select * from Card where CardID=‘"+txtCard.Text+"‘and CardPass=‘"+txtPass.Text+"‘";
string consql=ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
SqlConnection con = new SqlConnection(consql);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["uname"] = txtCard.Text;//把帐号存到Session中
Response.Redirect("Default.aspx");
}
else
{
Label1.Text = "帐户名或密码有错!";
}
con.Close();
}
    其次在首页一加载时写
protected void Page_Load(object sender, EventArgs e)
{
if (Session["uname"] == null)
{
            Response.Redirect("Login.aspx");//得到Session中的值,如果为空则跳转到登陆页面重新登陆       
}
}
    然后在查询余额页面中写

protected void Page_Load(object sender, EventArgs e)
{
string consql=ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
if (Session["uname"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
string sql = "select Moneys from Card where CardID=‘"+Session["uname"].ToString()+"‘";
SqlConnection con = new SqlConnection(consql);
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Label1.Text =Convert.ToInt32(dr[0]).ToString();//用Label接显示户的余额
}
con.Close();
}
}
    最后在转账页面中写
public partial class PostMoney : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["uname"] == null)
{
Response.Redirect("Login.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != Session["uname"].ToString())//判断是不是给自己转账{
string consql = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
string sql = "select CardID from Card where CardID=‘" + TextBox1.Text + "‘";//从数据库总查询要转账的帐号是否存在
SqlConnection con = new SqlConnection(consql);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())//如果存在
{
dr.Close();//网页设计数据阅读器打开后必须关闭cmd.CommandText="select Moneys from Card where CardID=‘"+Session["uname"].ToString()+"‘";//网页建设从数据库中查询自己的余额
SqlDataReader dr1 = cmd.ExecuteReader();
dr1.Read();
if (Convert.ToInt32(dr1[0]) > Convert.ToInt32(TextBox2.Text))//如果自己的余额大于要转账的数目
{
dr1.Close();
cmd.CommandText="updateCard setMoneys=Moneys-‘"+Convert.ToInt32(TextBox2.Text)+"‘where CardID=‘"+Session["uname"].ToString()+"‘";//更新数据库中钱的数目,减去所要转账的数目
if (cmd.ExecuteNonQuery() > 0)//假如更新成功
{
cmd.CommandText = "update Card set Moneys=Moneys+‘" + Convert.ToInt32(TextBox2.Text) + "‘where CardID=‘" + TextBox1.Text + "‘";//更新目标帐号的钱数,加上所要转账的数目
if (cmd.ExecuteNonQuery() > 0)
{
RegisterStartupScript("msg"," ");
}
else
{
RegisterStartupScript("msg"," ");
}
}
}
else
{
RegisterStartupScript("msg"," ");
}
con.Close();
}
else
{
RegisterStartupScript("msg", " ");
}
}
else
{
RegisterStartupScript("msg"," ");
}
}
}

    从以上的代码在网站制作太繁琐了,每次都要写连接数据库语句。在下一篇将介绍如何用三层来做这个项目。


RSS订阅| 关于我们| 网站地图| 加入项目| 法律条款| 友情链接| Firefox| Sitemap| 网站统计

全国免费咨询热线:400-600-6495 优惠活动咨询电话:021-62278496 Email:ajaxcms@yahoo.cn

上海慧云信息科技有限公司版权所有 2006-2009 沪ICP备09038165号

本站关键词:网站建设 高品质网站建设