ASP.net数据库相关操作

这是asp一个web相关代码,包括exp1.aspx主文件,修改页面UserEdit.aspx,以及登陆界面login.aspx.

原始代码---百度云
任务完成代码---百度云

主文件,c#相关代码,采用的是access数据库。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public partial class exp1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ShowGridView();
 
    }

      protected  void  ShowGridView()
    {
        //MyDb mydb = new MyDb();
        string ur = DropDownList1.SelectedValue;
        OleDbParameter[] para = { new OleDbParameter("ur", ur) };
        if (ur == "All")
            GridView1.DataSource = MyDb.getMyDb().GetData("select * from UserTB");
        else
            GridView1.DataSource = MyDb.getMyDb().GetData("select * from UserTB where UserRight=?", para);
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string u = TextBox1.Text; //用户名
        string p = TextBox2.Text;//密码
        string r = DropDownList2.Text;//权限
        string sql = "insert into UserTB(UserName,UserPassword,UserRight) values(?,?,?)";
        OleDbParameter[] para = { new OleDbParameter("u", u), new OleDbParameter("p", p), new OleDbParameter("r", r) };
        MyDb.getMyDb().cud(sql, para);
        ShowGridView();
        
    }



    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {

        int id = (int)e.Keys["UserID"];
        string sql = "delete from UserTB where UserID=?";
        OleDbParameter[] para = { new OleDbParameter("id", id) };
        MyDb.getMyDb().cud(sql, para);
        ShowGridView();
    }

    
}

删除,采用控件

设置属性值

ASP.net数据库相关操作_第1张图片
delete属性.jpg

单击如下按钮,写code

ASP.net数据库相关操作_第2张图片
delete双击.jpg

code

 protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {

        int id = (int)e.Keys["UserID"];
        string sql = "delete from UserTB where UserID=?";
        OleDbParameter[] para = { new OleDbParameter("id", id) };
        MyDb.getMyDb().cud(sql, para);
        ShowGridView();
    }

在变成模板,设置事件

ASP.net数据库相关操作_第3张图片
delete弹窗.jpg

写一个弹窗事件

ASP.net数据库相关操作_第4张图片
delete 函数.jpg



修改,控件操作
ASP.net数据库相关操作_第5张图片
修改.PNG

UserEdit相关代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class UserEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            OleDbDataReader dr = MyDb.getMyDb().GetData("Select * from UserTB where UserID=" + id);
            dr.Read();
            TextBox1.Text = dr["UserID"].ToString();
            TextBox2.Text = dr["UserName"].ToString();
            TextBox3.Text = dr["UserPassword"].ToString();
            DropDownList1.Text = dr["UserRight"].ToString();
            TextBox4.Text = dr["RegDate"].ToString();
        }

    }





    protected void Button1_Click(object sender, EventArgs e)
    {


        int id = int.Parse(TextBox1.Text);
        string u = TextBox2.Text;
        string p = TextBox3.Text;
        string r = DropDownList1.Text;
        DateTime d = DateTime.Parse(TextBox4.Text);
        string sql = "update UserTB  SET UserName =?,UserPassword =?,UserRight=?,RegDate=? where UserID=?";

        OleDbParameter[] para = { new OleDbParameter("u",u),     new OleDbParameter("p",p),
            new OleDbParameter("r",r),new OleDbParameter("d",d), new OleDbParameter("id",id)
        };

        MyDb.getMyDb().cud(sql, para);
        Response.Redirect("exp1.aspx");

    }
}

还有一个登陆界面相关的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string uname = TextBox1.Text;
        string pwd = TextBox2.Text;
        OleDbParameter[] para = {new OleDbParameter("uname",uname),new OleDbParameter("pwd",pwd) };
        OleDbDataReader dr = MyDb.getMyDb().GetData("select * from UserTB where UserName=? and UserPassword=?",para);
        if (!dr.Read())//dr里没有记录
            Label1.Text = "用户名或密码错误!";
        else
        {
            Session["UserName"] = dr["UserName"].ToString();
            Response.Redirect("default.aspx");
        }
    }
}

草稿

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class stuEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            OleDbDataReader dr = MyDb.getMyDb().GetData("select * from 学生表 where 学号 = " + id);
            dr.Read();
            TextBox1.Text = dr["学号"].ToString();
            TextBox2.Text = dr["姓名"].ToString();
            DropDownList1.Text = dr["性别"].ToString();
            DropDownList2.Text = dr["院系"].ToString();
            TextBox3.Text = dr["出生日期"].ToString();
            TextBox4.Text = dr["身份证号"].ToString();


        }



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       //   string id = TextBox1.Text;
         long id = long.Parse(TextBox1.Text);
         string u = TextBox2.Text;
         string sex = DropDownList1.Text;
         string  yx= DropDownList2.Text;
         DateTime bir =DateTime.Parse( TextBox3.Text);
         string sfz = TextBox4.Text;

         string sql = "update 学生表 SET 姓名 =?,性别 =?,院系 =? where 学号=?";

         OleDbParameter[] para = { new OleDbParameter("u",u),     new OleDbParameter("sex",sex),
            new OleDbParameter("yx",yx), new OleDbParameter("id",id)
        };

         MyDb.getMyDb().cud(sql, para);
       //   Response.Redirect("exp1.aspx");
    }
}
 protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        /*    
       重点提醒: 
GridView如果不绑定数据源控件,则以下两种方式,无法获取主键: 
e.Keys[0].ToString() 
e.Keys["id"].ToString() 
请使用以下方式获取主键: 
gv.DataKeys[e.RowIndex].Value
        
 */

        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());


        string sql = "delete from 学生表 where 学号 =?";
        OleDbParameter[] para = { new OleDbParameter("id", id) };
        MyDb.getMyDb().cud(sql, para);
        ShowGridView();
    }
}

你可能感兴趣的