Asp.net增删改查的基本操作(最简单操作)

本文介绍了一个使用ASP.NET实现的简单班级管理系统,通过SQL Server数据库进行数据存取操作。该系统支持班级信息的增删改查,并能显示所选班级的具体信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select * from class", con);
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        con.Open();
        DataSet ds = new DataSet();
        ad.Fill(ds);
        ds.Tables[0].Columns[0].ColumnName = "班级ID";
        ds.Tables[0].Columns[1].ColumnName = "班级";
        ds.Tables[0].Columns[2].ColumnName = "班级位置";

        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();

        this.txtClassId.Text = this.GridView1.Rows[0].Cells[1].Text.ToString();
        this.txtClassName.Text = this.GridView1.Rows[0].Cells[2].Text.ToString().Trim();
        this.txtClassLocation.Text = this.GridView1.Rows[0].Cells[3].Text.ToString().Trim();
        this.GridView1.Rows[0].RowState = DataControlRowState.Selected;

        con.Close();

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridView1.SelectedRow.RowIndex != 0)
        {
            this.GridView1.Rows[0].RowState = DataControlRowState.Normal;
        }
        this.txtClassId.Text = GridView1.SelectedRow.Cells[1].Text.ToString();
        this.txtClassName.Text = GridView1.SelectedRow.Cells[2].Text.ToString().Trim();
        this.txtClassLocation.Text = GridView1.SelectedRow.Cells[3].Text.ToString().Trim();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
        string sql = "insert into class values ('" + this.txtClassId.Text + "', '" + this.txtClassName.Text + "', '" + this.txtClassLocation.Text + "')";
        SqlCommand cmd = new SqlCommand(sql, con);

        con.Open();
        cmd.ExecuteNonQuery();

        cmd.CommandText = "select * from class";
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();

        con.Close();

        Response.Redirect(Request.RawUrl);
    }

    protected void btnChange_Click(object sender, EventArgs e)
    {
        int oldcode = Convert.ToInt32(this.GridView1.SelectedRow.Cells[1].Text);
        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
        string sql = "update class set classname = '" + this.txtClassName.Text + "', classlocation = '" + this.txtClassLocation.Text
            + "'where cid = " + oldcode;
        SqlCommand cmd = new SqlCommand(sql, con);

        con.Open();
        cmd.ExecuteNonQuery();

        cmd.CommandText = "select * from class";
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();

        con.Close();

        Response.Redirect(Request.RawUrl);
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        int oldcode = Convert.ToInt32(this.GridView1.SelectedRow.Cells[1].Text);
        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
        string sql = "delete from class where cid = " + oldcode;
        SqlCommand cmd = new SqlCommand(sql, con);

        con.Open();
        cmd.ExecuteNonQuery();

        cmd.CommandText = "select * from class";
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();

        con.Close();

        Response.Redirect(Request.RawUrl);
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值