OleDbCommandBuilder批量更新数据库[Session的应用]

//为了便于阅读写的有些地方有点重复,见谅

//Web.config数据库链接字符串

 
   
 

//前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default4.aspx.cs" ValidateRequest="false" Inherits="RM.WebApp.default4" %>



    无标题页


   


   

       
           
               
                   
                       
                   

                   
                       
                   

               

               
                   
                       
                   

                   
                       
                   

               

               
                   
                                                    TextMode="MultiLine" Width="215px">
                   

                   
                       
                   

               

               
                   
                        更新
                        取消
                   

                   
                        编辑
                        删除
                   

               

           

       

       

       

         

   


//后台代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.OleDb;

namespace RM.WebApp
{
    public partial class default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds=new DataSet();
                bind(ds);
            }
        }

        protected void gdv1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName.ToString())
            {
                case "Medit":
                    GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    gdv1.EditIndex = gvr.RowIndex;
                    DataSet ds = (DataSet)Session["bind"];
                    bind(ds);
                    break;
                case "Mcancel":
                    DataSet dt = (DataSet)Session["bind"];
                    gdv1.EditIndex = -1;
                    bind(dt);
                    break;
                case "Mdelete":
                    DataSet dsD= (DataSet)Session["bind"];
                    GridViewRow gvrq = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    int i = gvrq.RowIndex;
                    dsD.Tables["rm_Article"].Rows.Remove(dsD.Tables["rm_Article"].Rows[i]);
                    bind(dsD);
                    break;
                case "Mupdata":
                    GridViewRow g= (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    int x = g.RowIndex;
                    DataSet dsU = (DataSet)Session["bind"];
                    dsU.Tables["rm_Article"].Rows[x]["title"] = ((TextBox)gdv1.Rows[x].FindControl("cbxTitle")).Text;
                    dsU.Tables["rm_Article"].Rows[x]["content"] = ((TextBox)gdv1.Rows[x].FindControl("txtContent")).Text;
                    gdv1.EditIndex = -1;
                    bind(dsU);
                    break;
            }
        }

        public void bind(DataSet ds)
        {
            if (Session["bind"] == null)
            {
                OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["access"].ConnectionString);
                conn.Open();
                OleDbDataAdapter adp = new OleDbDataAdapter("select * from rm_Article", conn);
                DataSet dss = new DataSet();
                adp.Fill(dss, "rm_Article");
                gdv1.DataSource = dss.Tables["rm_Article"].DefaultView;
                gdv1.DataBind();

                Session["bind"] = dss;

            }
            else
            {
                gdv1.DataSource = ds.Tables["rm_Article"];
                gdv1.DataBind();
                Session["bind"] = ds;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            OleDbConnection oleCn = new OleDbConnection(ConfigurationManager.ConnectionStrings["access"].ConnectionString);
            string sql = "select * from rm_Article";
            OleDbDataAdapter oleSub = new OleDbDataAdapter(sql, oleCn);
            OleDbCommandBuilder cb1 = new OleDbCommandBuilder(oleSub);

            DataSet ds = (DataSet)Session["bind"];
            oleSub.Update(ds, "rm_Article");
            Response.Write("数据库更新成功");
        }
    }
}

你可能感兴趣的:((C#)ADO.NET,session,数据库,dataset,textbox,server,asp)