Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs
View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();//创建数据集
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
using (SqlConnection con = new SqlConnection(conStr))//创建数据连接
{
con.Open();//打开连接
SqlDataAdapter sda = new SqlDataAdapter("select * from UserInfo", con);//创建数据适配器
sda.Fill(ds, "UserInfo");//填充数据集
}
//第一行的UserName列值由"系统管理员"改为"temp"
ds.Tables["UserInfo"].Rows[0]["UserName"] = "temp";
//取第一行的UserName列的原值是"系统管理员"
string s = ds.Tables["UserInfo"].Rows[0]["UserName", DataRowVersion.Original].ToString();
//删除第二、第三行
ds.Tables["UserInfo"].Rows[2].Delete();
ds.Tables["UserInfo"].Rows[1].Delete();
//取出行状态是删除的行,也就是第二、第三行
DataRow[] delRows = ds.Tables["UserInfo"].Select("", "", DataViewRowState.Deleted);
//在页面中输出行状态是删除的行集合
foreach (DataColumn col in ds.Tables["UserInfo"].Columns)
{
Response.Write(col.ColumnName + " ");
}
Response.Write("<br/>");
foreach (DataRow row in delRows)
{
foreach (DataColumn col in ds.Tables["UserInfo"].Columns)
{
Response.Write(row[col, DataRowVersion.Original].ToString() + " ");
}
Response.Write("<br/>");
}
}
}