版权声明: https://blog.csdn.net/eds124/article/details/86546655
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 Dao()
{
}
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 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();
}
}
}