1. 程式人生 > >ASP.NET 登陸與註冊程式碼

ASP.NET 登陸與註冊程式碼

登陸後臺程式碼

protected void LoginSubmit_Click(object sender, EventArgs e)
{
    string uname = name.Text.Trim();
    string pwd = password.Text.Trim();
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    string sql = string.Format("select count(*) from userinfo where username='{0}' and password='{1}'", uname, pwd);
    SqlCommand command = new SqlCommand(sql, connection);
    connection.Open();
    int a = (int)command.ExecuteScalar();
    connection.Close();
    if (a > 0)  //登陸成功
    {
        Session["admin_user"] = uname;
    }
    else //登陸失敗
    {
        Response.Write("<script> alert('使用者名稱或密碼錯誤,請重新輸入')</script>");
    }
}

註冊後臺程式碼

protected void Registersubmit_Click(object sender, EventArgs e)
{

        string a1 = name.Text.Trim();
        string a2 = password.Text.Trim();
        string a3 = DateTime.Now.ToString();
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        string sql = string.Format("insert into register values( '{0}','{1}','{2}')", a1, a2, a3);
        SqlCommand command = new SqlCommand(sql, connection);
        connection.Open();
        int count = (int)command.ExecuteNonQuery();
        connection.Close();
        if (count > 0)
        {
            Response.Write("<script>alert('註冊成功')</script>");
        }
        else
        {
            Response.Write("<script>alert('註冊失敗,請您重試')</script>");
        }

}