史上最强mvc小三层file:///C:\Users\j\Desktop\WebSite\App_Code\Dao.cs

版权声明: https://blog.csdn.net/eds124/article/details/86547584
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;

public class Dao
{
    private static string ConnectionString = "Data Source=.;Initial Catalog=mydata;User ID=sa;Password=Abcdefg1";

    public static bool login(string username, string userpass)//登陆检查
    {
        bool b = false;
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            System.Data.SqlClient.SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "select count(*) from t_user where username = @username and userpass = @userpass";
            comm.Parameters.AddWithValue("username", username);
            comm.Parameters.AddWithValue("userpass", userpass);
            conn.Open();
            object o = comm.ExecuteScalar();
            int i = Convert.ToInt32(o);
            if (i > 0)
            {
                b = true;
            }
        }
        return b;
    }

    public static void login(System.Web.UI.WebControls.Login l, HttpRequest Request, HttpResponse Response)
    {
        string username = l.UserName;//接收控件用户名密码
        string userpass = l.Password;
        bool b = l.RememberMeSet;//是否记住用户

        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            System.Data.SqlClient.SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "select count(*) from t_user where username = @username and userpass = @userpass";
            comm.Parameters.AddWithValue("username", username);
            comm.Parameters.AddWithValue("userpass", userpass);
            conn.Open();
            object o = comm.ExecuteScalar();
            int i = Convert.ToInt32(o);
            if (i > 0)
            {
                Response.Cookies["username"].Value = username;
                Response.Cookies["userpass"].Value = userpass;
                if (b)
                {
                    Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
                    Response.Cookies["userpass"].Expires = DateTime.Now.AddDays(7);
                }
                Response.Redirect("~/Index.aspx");
            }
        }
    }

    public static void a(HttpRequest Request, HttpResponse Response)
    {
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            try
            {
                System.Data.SqlClient.SqlCommand comm = conn.CreateCommand();
                comm.CommandText = "select a from t_user where username = @username and userpass = @userpass";
                string username = Request.Cookies["username"].Value;
                string userpass = Request.Cookies["userpass"].Value;
                comm.Parameters.AddWithValue("username", username);
                comm.Parameters.AddWithValue("userpass", userpass);
                conn.Open();
                object o = comm.ExecuteScalar();
                int i = Convert.ToInt32(o);
                if (i < 0)
                {
                    Response.Redirect("~/Index.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Index.aspx");
            }
        }
    }

    public static void Level(HttpRequest Request, HttpResponse Response, string s)
    {
        Dictionary<string, string> dsssql = new Dictionary<string, string>();
        dsssql.Add("a", "select a from t_user where username = @username and userpass = @userpass");
        dsssql.Add("b", "select b from t_user where username = @username and userpass = @userpass");
        dsssql.Add("c", "select c from t_user where username = @username and userpass = @userpass");
        dsssql.Add("d", "select d from t_user where username = @username and userpass = @userpass");
        dsssql.Add("e", "select e from t_user where username = @username and userpass = @userpass");
        dsssql.Add("f", "select f from t_user where username = @username and userpass = @userpass");
        dsssql.Add("g", "select g from t_user where username = @username and userpass = @userpass");
        dsssql.Add("h", "select h from t_user where username = @username and userpass = @userpass");
        dsssql.Add("i", "select i from t_user where username = @username and userpass = @userpass");
        dsssql.Add("j", "select j from t_user where username = @username and userpass = @userpass");
        dsssql.Add("k", "select k from t_user where username = @username and userpass = @userpass");
        dsssql.Add("l", "select l from t_user where username = @username and userpass = @userpass");
        dsssql.Add("m", "select m from t_user where username = @username and userpass = @userpass");
        dsssql.Add("n", "select n from t_user where username = @username and userpass = @userpass");
        dsssql.Add("o", "select o from t_user where username = @username and userpass = @userpass");
        dsssql.Add("p", "select p from t_user where username = @username and userpass = @userpass");
        dsssql.Add("q", "select q from t_user where username = @username and userpass = @userpass");
        dsssql.Add("r", "select r from t_user where username = @username and userpass = @userpass");
        dsssql.Add("s", "select s from t_user where username = @username and userpass = @userpass");
        dsssql.Add("t", "select t from t_user where username = @username and userpass = @userpass");
        dsssql.Add("u", "select u from t_user where username = @username and userpass = @userpass");
        dsssql.Add("v", "select v from t_user where username = @username and userpass = @userpass");
        dsssql.Add("w", "select w from t_user where username = @username and userpass = @userpass");
        dsssql.Add("x", "select x from t_user where username = @username and userpass = @userpass");
        dsssql.Add("y", "select y from t_user where username = @username and userpass = @userpass");
        dsssql.Add("z", "select z from t_user where username = @username and userpass = @userpass");

        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            try
            {
                System.Data.SqlClient.SqlCommand comm = conn.CreateCommand();
                comm.CommandText = dsssql[s];
                string username = Request.Cookies["username"].Value;
                string userpass = Request.Cookies["userpass"].Value;
                comm.Parameters.AddWithValue("username", username);
                comm.Parameters.AddWithValue("userpass", userpass);
                conn.Open();
                object o = comm.ExecuteScalar();
                int i = Convert.ToInt32(o);
                if (i < 1)
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Default.aspx");
            }
        }
    }

    public static void BangDingUser(System.Web.UI.WebControls.GridView gv)
    {
        using (System.Web.UI.WebControls.SqlDataSource sds = new System.Web.UI.WebControls.SqlDataSource(Dao.ConnectionString, "select * from t_user order by username asc"))
        {
            gv.DataSource = sds;
            gv.DataBind();
        }
    }

    public static void GridViewUser(System.Web.UI.WebControls.GridView gv)
    {
        gv.Caption = "用户表";
        gv.EmptyDataText = "用户表数据为空";
        gv.EmptyDataRowStyle.BackColor = System.Drawing.Color.Red;
        gv.GridLines = System.Web.UI.WebControls.GridLines.None;
        gv.AllowPaging = true;
        gv.HeaderStyle.BackColor = System.Drawing.Color.Green;
        gv.AlternatingRowStyle.BackColor = System.Drawing.Color.Blue;
        gv.RowStyle.BackColor = System.Drawing.Color.Yellow;
        gv.SelectedRowStyle.BackColor = System.Drawing.Color.Pink;
        gv.AutoGenerateDeleteButton = true;
        gv.AutoGenerateEditButton = true;
        gv.AutoGenerateSelectButton = true;
        gv.DataKeyNames = new string[] { "p_user_id" };
    }

    public static void UpdateUser(System.Web.UI.WebControls.GridView gv, int i)
    {
        System.Web.UI.WebControls.TextBox tb_username = (System.Web.UI.WebControls.TextBox)gv.Rows[i].Cells[1].Controls[0];
        System.Web.UI.WebControls.TextBox tb_userpass = (System.Web.UI.WebControls.TextBox)gv.Rows[i].Cells[2].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_a = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[3].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_b = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[4].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_c = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[5].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_d = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[6].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_e = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[7].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_f = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[8].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_g = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[9].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_h = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[10].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_i = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[11].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_j = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[12].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_k = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[13].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_l = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[14].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_m = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[15].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_n = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[16].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_o = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[17].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_p = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[18].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_q = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[19].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_r = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[20].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_s = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[21].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_t = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[22].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_u = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[23].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_v = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[24].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_w = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[25].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_x = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[26].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_y = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[27].Controls[0];
        System.Web.UI.WebControls.CheckBox cb_z = (System.Web.UI.WebControls.CheckBox)gv.Rows[i].Cells[28].Controls[0];
        string p_userid = gv.DataKeys[i].Values["p_user_id"].ToString();

        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            System.Data.SqlClient.SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "update t_user set username = @username, userpass = @userpass, a = @a, b = @b, c = @c, d = @d, e = @e, f = @f, g = @g, h = @h, i = @i, j = @j, k = @k, l = @l, m = @m, n = @n, o = @o, p = @p, q = @q, r = @r, s = @s, t = @t, u = @u, v = @v, w = @w, x = @x, y = @y, z = @z where p_user_id = @p_user_id";
            comm.Parameters.AddWithValue("username", tb_username.Text);
            comm.Parameters.AddWithValue("userpass", tb_userpass.Text);
            comm.Parameters.AddWithValue("a", cb_a.Checked);
            comm.Parameters.AddWithValue("b", cb_b.Checked);
            comm.Parameters.AddWithValue("c", cb_c.Checked);
            comm.Parameters.AddWithValue("d", cb_d.Checked);
            comm.Parameters.AddWithValue("e", cb_e.Checked);
            comm.Parameters.AddWithValue("f", cb_f.Checked);
            comm.Parameters.AddWithValue("g", cb_g.Checked);
            comm.Parameters.AddWithValue("h", cb_h.Checked);
            comm.Parameters.AddWithValue("i", cb_i.Checked);
            comm.Parameters.AddWithValue("j", cb_j.Checked);
            comm.Parameters.AddWithValue("k", cb_k.Checked);
            comm.Parameters.AddWithValue("l", cb_l.Checked);
            comm.Parameters.AddWithValue("m", cb_m.Checked);
            comm.Parameters.AddWithValue("n", cb_n.Checked);
            comm.Parameters.AddWithValue("o", cb_o.Checked);
            comm.Parameters.AddWithValue("p", cb_p.Checked);
            comm.Parameters.AddWithValue("q", cb_q.Checked);
            comm.Parameters.AddWithValue("r", cb_r.Checked);
            comm.Parameters.AddWithValue("s", cb_s.Checked);
            comm.Parameters.AddWithValue("t", cb_t.Checked);
            comm.Parameters.AddWithValue("u", cb_u.Checked);
            comm.Parameters.AddWithValue("v", cb_v.Checked);
            comm.Parameters.AddWithValue("w", cb_w.Checked);
            comm.Parameters.AddWithValue("x", cb_x.Checked);
            comm.Parameters.AddWithValue("y", cb_y.Checked);
            comm.Parameters.AddWithValue("z", cb_z.Checked);
            comm.Parameters.AddWithValue("p_user_id", p_userid);
            conn.Open();
            comm.ExecuteNonQuery();
            gv.EditIndex = -1;
        }
    }

    public static void DeleteUser(System.Web.UI.WebControls.GridView gv, int i)
    {
        string p_userid = gv.DataKeys[i].Values["p_user_id"].ToString();
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            System.Data.SqlClient.SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "delete t_user  where p_user_id = @p_user_id";
            comm.Parameters.AddWithValue("p_user_id", p_userid);
            conn.Open();
            comm.ExecuteNonQuery();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/eds124/article/details/86547584