日期:2014-05-17  浏览次数:20538 次

关于登陆的代码简写
下面是我的登陆的代码哦。。=w=感觉有点不规范,现在我的这些代码是写在后台的,如果我想利用三层的知识,应该怎么改写好哦,或者大家还有什么登陆的好方法,能写下给我看看吗?

public int checklogin(string U_LoginName, string U_Password)
        {
            SqlConnection con = new SqlConnection(DBhelper.connectionString);
            SqlCommand myCommand = new SqlCommand("select count(U_LoginName) from sys_User where U_LoginName=@username and U_Password=@password and (U_Type>2 or U_Type = 0) ", con);
            myCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 50));
            myCommand.Parameters["@username"].Value = U_LoginName;
            myCommand.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
            myCommand.Parameters["@password"].Value = Common.md5(U_Password,32);

            myCommand.Connection.Open();
            int i = (int)myCommand.ExecuteScalar();
            myCommand.Connection.Close();
            return i;
        }

        protected void denglu_Click(object sender, ImageClickEventArgs e)
        {
            int o = this.checklogin(txtusername.Text, txtpassword.Text);
            if (o > 0)
            {
                Session["username"] = txtusername.Text;


                Response.Write("<script> alert('登陆成功'); window.location.href='ProjectList.aspx';</script>");
            }
            else
            {
                Response.Write("<script> alert('登陆失败,账号密码有误或者权限不够');</script>");
            }
        }


------解决方案--------------------
可以学习下petshop的源码。http://download.csdn.net/detail/xinhua8hao/3066476