/*UpdateDelForm.aspx
前台程序代码
*/
AutoEventWireup="false" Inherits="UpdateDelDbDemo.UpdateDelForm" %>
WebForm1ButtonType="LinkButton" UpdateText="更新" CancelText="取消"
EditText="编辑">
/*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
{
///
/// WebForm1 的摘要说明。
///
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 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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
//读取数据并帮定到DataGrid
private void LoadGrid()
{
Connect();
SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);
DataSet ds=new DataSet();
objAdpt.Fill(ds,"dtUserInfo");
dgDemo.DataSource=ds;
dgDemo.DataBind();
}
//建立数据库的连接
private void Connect()
{
if(objCon==null)
objCon=new SqlConnection(strCon);
if(objCon.State==ConnectionState.Closed)
objCon.Open();
}
//断开数据库的连接
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;
//此方法具体处理数据库的更新
UpdateRecord(strUserID,strFirstName,strLastName,strAddress);
//调用此方法从数据库读取数据
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"];
//设定表的主键
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);
//调用此方法从数据库读取数据
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"];
//设定表的主键
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 发表评论
文章来源:http://www.blogjava.net/kuxiaoku/articles/94807.html
posted on 2007-01-19 00:15 苦笑枯 阅读(310) 评论(0) 编辑 收藏 所属分类: C#