posts - 56,  comments - 12,  trackbacks - 0

          /*UpdateDelForm.aspx

          前臺程序代碼

          */

          <%@ Page language="c#" Codebehind="UpdateDelForm.aspx.cs" AutoEventWireup="false" Inherits="UpdateDelDbDemo.UpdateDelForm" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
          <HTML>
           <HEAD>
            <title>WebForm1</title>
            <meta name="vs_snapToGrid" content="False">
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" Content="C#">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content=" </HEAD>
           <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
             <FONT face="宋體">
              <asp:DataGrid id="dgDemo" runat="server" AutoGenerateColumns="False" GridLines="Horizontal">
               <Columns>
                <asp:BoundColumn DataField="ID" ReadOnly="True" HeaderText="UserID"></asp:BoundColumn>
                <asp:BoundColumn DataField="FirstName" HeaderText="FirstName"></asp:BoundColumn>
                <asp:BoundColumn DataField="LastName" HeaderText="LastName"></asp:BoundColumn>
                <asp:BoundColumn DataField="address" HeaderText="Address"></asp:BoundColumn>
                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="編輯"></asp:EditCommandColumn>
                <asp:ButtonColumn Text="刪除" CommandName="Delete"></asp:ButtonColumn>
               </Columns>
              </asp:DataGrid></FONT>
            </form>
           </body>
          </HTML>

          /*UpdateDelForm.aspx.cs

          后臺程序代碼

          */

          using System;
          using System.Collections;
          using System.ComponentModel;
          using System.Data;
          using System.Data.SqlClient;
          using System.Drawing;
          using System.Web;
          using System.Web.SessionState;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.HtmlControls;

          namespace UpdateDelDbDemo
          {
           /// <summary>
           /// WebForm1 的摘要說明。
           /// </summary>
           public class UpdateDelForm : System.Web.UI.Page
           {
            protected System.Web.UI.WebControls.DataGrid dgDemo;
            private string strCon="server=JOSEN;database=testDb;integrated security=true";
            private string strSqlSelect="select * from UserInfo";
            private SqlConnection objCon;
           
            private void Page_Load(object sender, System.EventArgs e)
            {
             // 在此處放置用戶代碼以初始化頁面
             if(!IsPostBack)
              LoadGrid();
            }

            #region Web 窗體設(shè)計器生成的代碼
            override protected void OnInit(EventArgs e)
            {
             //
             // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
             //
             InitializeComponent();
             base.OnInit(e);
            }
            
            /// <summary>
            /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
            /// 此方法的內(nèi)容。
            /// </summary>
            private void InitializeComponent()
            {   
             this.dgDemo.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_CancelCommand);
             this.dgDemo.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_EditCommand);
             this.dgDemo.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_UpdateCommand);
             this.dgDemo.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_DeleteCommand);
             this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
            //讀取數(shù)據(jù)并幫定到DataGrid
            private void LoadGrid()
            {
             Connect();
             SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
             DataSet ds=new DataSet();
             objAdpt.Fill(ds,"dtUserInfo");

             dgDemo.DataSource=ds;
             dgDemo.DataBind();
            }
            //建立數(shù)據(jù)庫的連接
            private void Connect()
            {
             if(objCon==null)
              objCon=new SqlConnection(strCon);
             if(objCon.State==ConnectionState.Closed)
              objCon.Open();
            
            }
            //斷開數(shù)據(jù)庫的連接
            private void Disconnect()
            {
             objCon.Close();
            }

            private void dgDemo_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
             dgDemo.EditItemIndex=e.Item.ItemIndex;
             LoadGrid();
            }

            private void dgDemo_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
             dgDemo.EditItemIndex=-1;
             LoadGrid();
            }

            private void dgDemo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
             string strUserID=e.Item.Cells[0].Text;
             //string strUserID=((TextBox)e.Item.Cells[0].Controls[0]).Text;
             string strFirstName=((TextBox)e.Item.Cells[1].Controls[0]).Text;
             string strLastName=((TextBox)e.Item.Cells[2].Controls[0]).Text;
             string strAddress=((TextBox)e.Item.Cells[3].Controls[0]).Text;

             dgDemo.EditItemIndex=-1;
             //此方法具體處理數(shù)據(jù)庫的更新
             UpdateRecord(strUserID,strFirstName,strLastName,strAddress);
             //調(diào)用此方法從數(shù)據(jù)庫讀取數(shù)據(jù)
             LoadGrid();

            }

            private void UpdateRecord(string strUserID, string strFirstName, string strLastName, string strAddress)
            {
             Connect();
             SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
             DataSet ds=new DataSet();
             objAdpt.Fill(ds,"dtUserInfo");
             Disconnect();

             DataTable tbl=ds.Tables["dtUserInfo"];
             
             //設(shè)定表的主鍵
             DataColumn[] myKey=new DataColumn[1];
             myKey[0]=tbl.Columns["ID"];
             tbl.PrimaryKey=myKey;
             
             DataRow dr=tbl.Rows.Find(strUserID);
             //dr["ID"]=strUserID;
             dr["FirstName"]=strFirstName;
             dr["LastName"]=strLastName;
             dr["address"]=strAddress;

             SqlCommandBuilder cb=new SqlCommandBuilder(objAdpt);
             Connect();
             objAdpt.Update(ds,"dtUserInfo");
             Disconnect();
            }

            private void dgDemo_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {

             string strUserID=e.Item.Cells[0].Text;

             dgDemo.EditItemIndex=-1;
             DelRecord(strUserID);
             //調(diào)用此方法從數(shù)據(jù)庫讀取數(shù)據(jù)
             LoadGrid();
            }
            private void DelRecord(string strUserID)
            {
             Connect();
             SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
             DataSet ds=new DataSet();
             objAdpt.Fill(ds,"dtUserInfo");
             Disconnect();

             DataTable tbl=ds.Tables["dtUserInfo"];
             
             //設(shè)定表的主鍵
             DataColumn[] myKey=new DataColumn[1];
             myKey[0]=tbl.Columns["ID"];
             tbl.PrimaryKey=myKey;
             
             DataRow dr=tbl.Rows.Find(strUserID);
             dr.Delete();

             SqlCommandBuilder cb=new SqlCommandBuilder(objAdpt);
             Connect();
             objAdpt.Update(ds,"dtUserInfo");
             Disconnect();
            }
           }
          }


          苦笑枯 2007-01-19 00:15 發(fā)表評論

          文章來源:http://www.aygfsteel.com/kuxiaoku/articles/94807.html
          posted on 2007-01-19 00:15 苦笑枯 閱讀(359) 評論(0)  編輯  收藏 所屬分類: C#
          收藏來自互聯(lián)網(wǎng),僅供學習。若有侵權(quán),請與我聯(lián)系!

          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(2)

          隨筆分類(56)

          隨筆檔案(56)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乐平市| 宁晋县| 古蔺县| 鄢陵县| 平南县| 图木舒克市| 桦南县| 西昌市| 洮南市| 乐至县| 新竹市| 河津市| 鹤庆县| 广西| 台州市| 齐河县| 集安市| 龙游县| 马鞍山市| 枞阳县| 稷山县| 岚皋县| 都兰县| 台中市| 涟源市| 绩溪县| 吐鲁番市| 哈密市| 湖州市| 满城县| 县级市| 新竹县| 吉水县| 司法| 曲阜市| 扎鲁特旗| 贞丰县| 涞源县| 通许县| 博白县| 阳春市|