create
table
demo ( id_user
int
, name_user
varchar
(
30
) )
CREATE
PROCEDURE
Adddemo (
@id_user
int
,
@name_user
varchar
(
30
) )
AS
INSERT
INTO
demo (id_user,name_user)
VALUES
(
@id_user
,
@name_user
)
GO
CREATE
PROCEDURE
Deletedemo (
@id_user
int
)
AS
DELETE
demo
WHERE
id_user
=
@id_user
GO
CREATE
PROCEDURE
Updatedemo (
@id_user
int
,
@name_user
nvarchar
(
30
) )
AS
UPDATE
demo
SET
name_user
=
@name_user
WHERE
id_user
=
@id_user
IF
@@ROWCOUNT
>
0
--
This statement is used to update the DataSet if changes are done on the updated record (identities, timestamps or triggers )
SELECT
id_user,name_user
FROM
demo
WHERE
id_user
=
@id_user
GO
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
Microsoft.Practices.EnterpriseLibrary.Data;
using
Microsoft.Practices.EnterpriseLibrary.Data.Sql;
namespace
WebApplication12
{ /**/ /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load( object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if ( ! this .IsPostBack) { bind_data(); } } private void bind_data() { Microsoft.Practices.EnterpriseLibrary.Data.Database db; db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(); DataSet ds = db.ExecuteDataSet(System.Data .CommandType.Text, " Select * from demo " ); this .DataGrid1 .DataSource = ds.Tables[ 0 ]; this .DataGrid1.DataBind (); } Web 窗体设计器生成的代码 #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base .OnInit(e); } /**/ /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this .DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler( this .DataGrid1_EditCommand); this .Load += new System.EventHandler( this .Page_Load); } #endregion public void DataGrid1_EditCommand( object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { this .DataGrid1 .EditItemIndex = e.Item .ItemIndex ; bind_data(); } public void Establish_Commands(Database db,DataSet productsDataSet) { DBCommandWrapper insertCommandWrapper = db.GetStoredProcCommandWrapper( " Adddemo " ); insertCommandWrapper.AddInParameter( " @id_user " , DbType.Int32, " id_user " , DataRowVersion.Current); insertCommandWrapper.AddInParameter( " @name_user " , DbType.String, " name_user " , DataRowVersion.Current); DBCommandWrapper deleteCommandWrapper = db.GetStoredProcCommandWrapper( " Deletedemo " ); deleteCommandWrapper.AddInParameter( " @id_user " , DbType.Int32, " id_user " , DataRowVersion.Current); DBCommandWrapper updateCommandWrapper = db.GetStoredProcCommandWrapper( " Updatedemo " ); updateCommandWrapper.AddInParameter( " @id_user " , DbType.Int32, " id_user " , DataRowVersion.Current); updateCommandWrapper.AddInParameter( " @name_user " , DbType.String, " name_user " , DataRowVersion.Current); int rowsAffected = db.UpdateDataSet(productsDataSet, " demo " , insertCommandWrapper, updateCommandWrapper, deleteCommandWrapper, UpdateBehavior.Standard); } public void delete ( object sender, System.EventArgs e) { foreach (DataGridItem item in this .DataGrid1.Items) { CheckBox del = ((CheckBox)item.Cells[ 3 ].FindControl( " del " )) ; if (del.Checked == true ) { this .Response .Write ( " <script>alert(' " + this .DataGrid1.DataKeys[item.ItemIndex] + " ')</script> " ); Database db = DatabaseFactory.CreateDatabase(); DataSet productsDataSet = new DataSet(); string sqlCommand = " Select * " + " From demo " ; DBCommandWrapper dbCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand); string productsTable = " demo " ; db.LoadDataSet(dbCommandWrapper, productsDataSet, productsTable); DataTable table = productsDataSet.Tables[productsTable]; DataColumn[] keys = new DataColumn[ 1 ]; keys[ 0 ] = table.Columns[ " id_user " ] ; table.PrimaryKey = keys; DataRow dr = table.Rows.Find ( this .DataGrid1.DataKeys[item.ItemIndex]); dr.Delete (); Establish_Commands(db,productsDataSet); } } bind_data(); } public void add_new ( object src,DataGridCommandEventArgs e) { if (e.CommandName == " add " ) { Database db = DatabaseFactory.CreateDatabase(); DataSet productsDataSet = new DataSet(); string sqlCommand = " Select * " + " From demo " ; DBCommandWrapper dbCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand); string productsTable = " demo " ; db.LoadDataSet(dbCommandWrapper, productsDataSet, productsTable); DataTable table = productsDataSet.Tables[productsTable]; string id = ((TextBox)e.Item.Cells[ 2 ].FindControl( " add_id " )).Text; string temp = ((TextBox)e.Item.Cells[ 2 ].FindControl( " Textbox1 " )).Text; DataRow addedRow = table.Rows.Add( new object [] {id,temp} ); Establish_Commands(db,productsDataSet); bind_data(); } } } }
<%
@ Page language = " c# " Codebehind = " WebForm1.aspx.cs " AutoEventWireup = " false " Inherits = " WebApplication12.WebForm1 "
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
WebForm1
</
title
>
<
meta
content
="Microsoft Visual Studio .NET 7.1"
name
="GENERATOR"
>
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
<
script
>
</
script
>
</
HEAD
>
<
body
MS_POSITIONING
="GridLayout"
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
FONT
face
="宋体"
></
FONT
>
<
asp:datagrid
id
="DataGrid1"
style
="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 8px"
runat
="server"
DataKeyField
="id_user"
AutoGenerateColumns
="False"
BorderColor
="#3366CC"
BorderStyle
="None"
BorderWidth
="1px"
BackColor
="White"
CellPadding
="4"
Width
="592px"
Height
="64px"
ShowFooter
="True"
OnItemCommand
="add_new"
>
<
FooterStyle
ForeColor
="#003399"
BackColor
="#99CCCC"
></
FooterStyle
>
<
SelectedItemStyle
Font-Bold
="True"
ForeColor
="#CCFF99"
BackColor
="#009999"
></
SelectedItemStyle
>
<
ItemStyle
ForeColor
="#003399"
BackColor
="White"
></
ItemStyle
>
<
HeaderStyle
Font-Bold
="True"
ForeColor
="#CCCCFF"
BackColor
="#003399"
></
HeaderStyle
>
<
Columns
>
<
asp:TemplateColumn
>
<
ItemTemplate
>
<%
#DataBinder. Eval (Container.DataItem, " id_user " )
%>
</
ItemTemplate
>
<
FooterTemplate
>
<
asp:TextBox
ID
="add_id"
Runat
="server"
></
asp:TextBox
>
</
FooterTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
>
<
ItemTemplate
>
<%
#DataBinder. Eval (Container.DataItem, " name_user " )
%>
</
ItemTemplate
>
<
FooterTemplate
>
<
asp:TextBox
ID
="Textbox1"
Runat
="server"
></
asp:TextBox
>
</
FooterTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
>
<
ItemTemplate
>
<
asp:Button
ID
="edit"
Runat
="server"
Text
="编辑"
CommandArgument
=""
></
asp:Button
>
</
ItemTemplate
>
<
FooterTemplate
>
<
asp:Button
ID
="add_data"
Runat
="server"
Text
="添加"
CommandName
="add"
></
asp:Button
>
</
FooterTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="删除"
>
<
ItemTemplate
>
<
asp:CheckBox
ID
="del"
Runat
="server"
></
asp:CheckBox
>
</
ItemTemplate
>
<
FooterTemplate
>
<
asp:Button
ID
="delall"
Runat
="server"
Text
="删除所选"
OnClick
="delete"
></
asp:Button
>
</
FooterTemplate
>
</
asp:TemplateColumn
>
</
Columns
>
<
PagerStyle
HorizontalAlign
="Left"
ForeColor
="#003399"
BackColor
="#99CCCC"
Mode
="NumericPages"
></
PagerStyle
>
</
asp:datagrid
></
form
>
</
body
>
</
HTML
>
效果如下
转载于:https://www.cnblogs.com/gwazy/archive/2005/07/10/189790.html