增、查代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class 练习2_main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (Session["urole"] == null || Session["urole"].ToString() != "admin")
{
Response.Redirect("Login.aspx");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string connstr = "Server=localhost;User ID=root;Password=;Database=hehehe;CharSet=utf8";
MySqlConnection conn = new MySqlConnection(connstr);
conn.Open();
string sql = "select * from myuser ";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = cmd.ExecuteReader();
/*
while (reader.Read())
{
string yhm = reader["uname"].ToString();
string mm = reader["upass"].ToString();
Literal1.Text += "用户名: " + yhm + " 密码: " + mm + "<br>";
}
*/
GridView1.DataSource = reader;
GridView1.DataBind();
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string myname = TextBox1.Text;
string mypass = TextBox2.Text;
string mypassSure = TextBox4.Text;
Boolean a = true;
string connstr = "Server=localhost;User ID=root;Password=;Database=hehehe;CharSet=utf8";
MySqlConnection conn = new MySqlConnection(connstr);
if (myname.ToString() == "" || myname.ToString() == null)
{
Response.Write("<script>alert(\"用户不能为空\");</script>");
}
else {
string sqluname = "select uname from myuser ";
conn.Open();
MySqlCommand cmd0 = new MySqlCommand(sqluname, conn);
MySqlDataReader reader = cmd0.ExecuteReader();
while (reader.Read())
{
string yhm = reader["uname"].ToString();
if (myname.ToString() == yhm) {
Response.Write("<script>alert(\"用户已经存在\");</script>");
a = false;
break;
}
}
conn.Close();
if ((mypass.ToString() == mypassSure.ToString())&&a==true)
{
conn.Open();
string sql = "insert into myuser values('" + myname + "','" + mypass + "')";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int res = cmd.ExecuteNonQuery();
if (res > 0)
{
Literal1.Text = "插入成功";
}
else
{
Literal1.Text = "插入失败";
}
conn.Close();
}
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string myname = TextBox3.Text;
string connstr = "Server=localhost;User ID=root;Password=;Database=hehehe;CharSet=utf8";
MySqlConnection conn = new MySqlConnection(connstr);
conn.Open();
string sql = "delete from myuser where uname='" + myname + "' ";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int del = cmd.ExecuteNonQuery();
if (del > 0)
{
Literal1.Text = "删除成功";
}
else
{
Literal1.Text = "删除失败";
}
conn.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
string connstr = "Server=localhost;User ID=root;Password=;Database=hehehe;CharSet=utf8";
MySqlConnection conn = new MySqlConnection(connstr);
conn.Open();
string sql = "select count(*) from myuser ";
MySqlCommand cmd = new MySqlCommand(sql, conn);
string res = cmd.ExecuteScalar().ToString();
Literal1.Text ="统计后为:"+ res;
conn.Close();
}
}
修改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class 练习2_update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["uname"] == null || Request.QueryString["uname"].ToString() == "")
{
Response.Redirect("main.aspx");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string uname = Request.QueryString["uname"].ToString();
string updatepass = TextBox1.Text;
string connstr = "Server=localhost;User ID=root;Password=;Database=hehehe;CharSet=utf8";
MySqlConnection conn = new MySqlConnection(connstr);
conn.Open();
string sql = "update myuser set upass='" + updatepass + "' where uname='" + uname + "'";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int res = cmd.ExecuteNonQuery();
if (res > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("main.aspx");
}
conn.Close();
}
}
删除
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class 练习2_delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["uname"] == null || Request.QueryString["uname"].ToString() == "")
{
Response.Redirect("main.aspx");
}
else
{
string uname = Request.QueryString["uname"].ToString();
string connstr = "Server=localhost;User ID=root;Password=;Database=hehehe;CharSet=utf8";
MySqlConnection conn = new MySqlConnection(connstr);
conn.Open();
string sql = "delete from myuser where uname='" + uname + "'";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int res = cmd.ExecuteNonQuery();
if (res > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("main.aspx");
}
conn.Close();
}
}
}
}