.net中数据库基本增删查改操作

增、查代码:

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();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值