。net 登陆注册修改账户实例

1、登陆  

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string name = Request["userid"];
        string pwd = Request["userPass"];
  


        //string constr = "server=.;database=myschool;integrated security=SSPI";
        string constr = "server=.;database=users;uid=sa;pwd=admin";
        //string constr = "data source=.;initial catalog=myschool;user id=sa;pwd=sa";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = com;
        sda.SelectCommand.Connection = con;
        string sql = "select * from [users].[dbo].[ChenDemoUser] where username = '" + name + "' and upwd='" + pwd + "'";
        sda.SelectCommand.CommandText =sql;


        con.Open();


        DataTable dt = new DataTable();
        sda.Fill(dt);


        if (dt.Rows.Count > 0)
        {
            HttpContext.Current.Response.Write("{data:1,Msg:登陆成功!}");
            HttpContext.Current.Response.End();
        }
        else {
            HttpContext.Current.Response.Write("{data:0,Msg=帐号密码错误或帐号不存在}");
            HttpContext.Current.Response.End();
        }
        con.Close();
 


    }




    public static string ToJson(object d)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Serialize(d);
    }




    public static string TableToJson(DataTable dt)
    {
        List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
        foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            foreach (DataColumn dc in dt.Columns)
            {
                result.Add(dc.ColumnName, dr[dc].ToString());
            }
            list.Add(result);
        }
        return ToJson(list);//调用Serializer方法 
    }
}


2、注册

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request["userid"];
        string pwd = Request["userPass"];
        string constr = "server=.;database=users;uid=sa;pwd=admin";
        //string constr = "data source=.;initial catalog=myschool;user id=sa;pwd=sa";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand();
        com.Connection = con;
        string sql = "insert into [users].[dbo].[ChenDemoUser] (username,upwd) values ('" + name + "','" + pwd + "')";
        com.CommandText =sql;
        con.Open();//打开数据库 
         int i = com.ExecuteNonQuery();
        if (i > 0)
        {
            HttpContext.Current.Response.Write("{data:1}");
            HttpContext.Current.Response.End();
        }
        else {
            HttpContext.Current.Response.Write("{data:0}");
            HttpContext.Current.Response.End();
        }
        con.Close();




    }




    public static string ToJson(object d)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Serialize(d);
    }




    public static string TableToJson(DataTable dt)
    {
        List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
        foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            foreach (DataColumn dc in dt.Columns)
            {
                result.Add(dc.ColumnName, dr[dc].ToString());
            }
            list.Add(result);
        }
        return ToJson(list);//调用Serializer方法 
    }
}


3、修改

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string username = Request["userid"];
        string Oldpwd = Request["userPass"];
        string NewPwd = Request["userNewPass"];


        //string constr = "server=.;database=myschool;integrated security=SSPI";
        string constr = "server=.;database=users;uid=sa;pwd=admin";
        //string constr = "data source=.;initial catalog=myschool;user id=sa;pwd=sa";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand();
        com.Connection = con;
        string newsql = "update [users].[dbo].[ChenDemoUser] set upwd='"+NewPwd+ "' where  username = '" + username + "' and upwd='" + Oldpwd + "'";
        com.CommandText = newsql;
        con.Open();
        int i = com.ExecuteNonQuery();
        if (i > 0)
        {
             HttpContext.Current.Response.Write("1");
             HttpContext.Current.Response.End();
           // Response.Redirect("Login.html",false);
          //  Server.Transfer("Login.html", true);
        }
        else {
            HttpContext.Current.Response.Write("0");
            HttpContext.Current.Response.End();
        }


        con.Close();


    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39685861/article/details/78558804