• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

注册登录系统的基本逻辑与结构——ASP.NET(C#)源代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 源代码:13033480群共享

一、登录界面的逻辑结构

1、基本的数据库连接代码:

    protected void btnConfirm_Click(object sender, EventArgs e)

    {

        bool blIsAuthenticated = false;

 

        string strConnection = ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString.ToString();

 

        SqlConnection conn = new SqlConnection();

        conn.ConnectionString = strConnection;

 

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = conn;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT hy_Username FROM T_Vip WHERE hy_Username='" + txtUserName.Text +"' AND hy_Password='" + txtPassword.Text+"'";

 

 

        conn.Open();

 

        SqlDataReader rdr = cmd.ExecuteReader();

        if (rdr.Read())

        {

            blIsAuthenticated = true;

        }

        rdr.Close();

        conn.Close();

 

 

        if (blIsAuthenticated == true)

        {

            Response.Redirect("Welcome.aspx");

        }

        else

        {

            Response.Write("<script>alert('用户名或密码错误!')</script>");

        }

}

这个代码中使用了一个布尔变量blIsAuthenticated,来判断数据库操作的成功与否,而没有在数据库操作过程中添加过多的逻辑结构,在数据库操作结束后,再根据这个变量,进行你想要的逻辑操作。

2try…catch…finally结构

    protected void btnConfirm_Click(object sender, EventArgs e)

    {

        bool blIsAuthenticated = false;

 

        string strConnection = ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString.ToString();

 

        SqlCommand cmd = new SqlCommand();

        SqlConnection conn = new SqlConnection(strConnection);

        try

        {

            conn.Open();

 

            cmd.Connection = conn;

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "SELECT hy_Username FROM T_Vip WHERE hy_Username='" + txtUserName.Text +"' AND hy_Password='" + txtPassword.Text +"'";

 

            SqlDataReader rdr = cmd.ExecuteReader();

            if (rdr.Read())

            {

                blIsAuthenticated = true;

            }

            rdr.Close();

        }

        finally

        {

            conn.Close();

        }

 

        if (blIsAuthenticated == true)

        {

            Session["UserName"] = txtUserName.Text;

            Response.Redirect("Welcome.aspx");

        }

        else

        {

            Response.Write("<script>alert('用户名或密码错误!')</script>");

        }

}

为了说明下面的using()结构,代码中没有用使用catch关键字,注册中有采用。这个结构的主要目的,是确保SqlConnection在使用后能及时Close(),并Dispose()

3using()结构

    public partial class Login3 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

        protected void btnConfirm_Click(object sender, EventArgs e)

        {

            bool blIsAuthenticated =false;

 

            string strConnection =ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString.ToString();

 

            SqlCommand cmd = new SqlCommand();

            using (SqlConnection conn =newSqlConnection(strConnection))

            {

                conn.Open();

 

                cmd.Connection = conn;

                cmd.CommandType = CommandType.Text;

                cmd.CommandText = "SELECT hy_Username FROM T_Vip WHERE hy_Username='" + txtUserName.Text +"' AND hy_Password='" + txtPassword.Text +"'";

 

                SqlDataReader rdr = cmd.ExecuteReader();

                if (rdr.Read())

                {

                    blIsAuthenticated = true;

                }

                rdr.Close();

            }

 

            if (blIsAuthenticated ==true)

            {

                Session["UserName"] = txtUserName.Text;

                Response.Redirect("Welcome.aspx");

            }

            else

            {

                Response.Write("<script>alert('用户名或密码错误!')</script>");

            }

        }

    }

简单地理解,using()结构,就是catch…finally结构的另一种形式而已。

二、登录界面的逻辑结构

        protected void btnRegist_Click(object sender, EventArgs e)

        {

            if (Page.IsValid)

            {

                string strConnection =ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString;

                string cmdText = "insert into t_Vip(hy_Username,hy_Password,hy_Name,hy_Sex,hy_Age,hy_Mobliephone,hy_Email,hy_QQ) values(@hy_Username,@hy_Password,@hy_Name,@hy_Sex,@hy_Age,@hy_Mobliephone,@hy_Email,@hy_QQ)";

 

                string username = txtUserName.Text;

                string password = txtPassword.Text;

                string truename = txtTrueName.Text;

                string sex = radlSex.SelectedValue;

                int age = Convert.ToInt16(txtAge.Text);

                string phone = txtPhone.Text;

                string email = txtEmail.Text;

                string qq = txtQQ.Text;

 

                SqlParameter[] parms ={

                new SqlParameter("@hy_Username", SqlDbType.VarChar, 20),

                new SqlParameter("@hy_Password", SqlDbType.VarChar, 20),

                new SqlParameter("@hy_Name", SqlDbType.VarChar, 20),

                new SqlParameter("@hy_Sex", SqlDbType.VarChar, 2),

                new SqlParameter("@hy_Age", SqlDbType.Int),

                new SqlParameter("@hy_Mobliephone", SqlDbType.VarChar,20),

                new SqlParameter("@hy_Email", SqlDbType.VarChar, 50),

                new SqlParameter("@hy_QQ", SqlDbType.VarChar, 20)};

 

                parms[0].Value = username;

                parms[1].Value = password;

                parms[2].Value = truename;

                parms[3].Value = sex;

                parms[4].Value = age;

                parms[5].Value = phone;

                parms[6].Value = email;

                parms[7].Value = qq;

 

 

                SqlCommand cmd = new SqlCommand();

                SqlConnection conn =newSqlConnection(strConnection);

                try

                {

                    if (conn.State !=ConnectionState.Open)

                        conn.Open();

 

                    cmd.Connection = conn;

                    cmd.CommandType = CommandType.Text;

                    cmd.CommandText = cmdText;

 

                    foreach (SqlParameter parmin parms)

                        cmd.Parameters.Add(parm);

 

                    cmd.ExecuteNonQuery();

                }

                catch

                {

                    Response.Redirect("ErrorMessage.aspx");

                }

                finally

                {

                    conn.Close();

                }

 

                Response.Redirect("Login.aspx");

            }

        }

}

登录界面的逻辑结构,主要多了一个SqlCommand对象要使用的SqlParmeter参数,这个变量,采用数组比较好,便于以后把数据库操作部分放在函数中,放在类中时的参数传递。

三、界面设计

1、配色方案:

190-80-87

221-144-150

243-213-215

 

 

 

#BE5057

#DD9096

#F3D5D7

                  

2、格式设计

主要遵循了这个几个原则:

1)对于象登录、注册这样局部的布局采用表格+CSS结构,事实上ASP.NET的很多Web控件,都采用的表格。

2)共性的样子,如配色方案的颜色都放在了CSS样式表中,个性的样式,及个别CSS不能设置的样式如表格的行高,使用了表格的属性。

3)按照Stephen Walther(《ASP.NET2.0揭秘》一书的作者)的说法,舍弃了皮肤文件设置样式的方法,而采用了cssClass,把控件的样式也放在了CSS中。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
(转)从零开始学习ASP.NETMVC1.0(一)开天辟地入门篇发布时间:2022-07-10
下一篇:
ASP.NET 程序优化发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap