|
|
|
|
|
|
SqlHelper 下载
页面部分:

<%@ 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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="128px" Width="410px" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="id" HeaderText="自动ID" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="b1" CommandName="set" Text="更新" runat="server" CommandArgument='<%# Eval("id")%>'/>
<asp:Button id="b2" CommandName="del" Text="删除" runat="server" CommandArgument='<%# Eval("id")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>




cs代码部分:

using System;
using System.Data;
using System.Configuration;
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 Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
  {
static string conn = "Data Source=03DC05A381DE461;Initial Catalog=abc;User ID=sa";

protected void Page_Load(object sender, EventArgs e)
 {
if (!Page.IsPostBack)
 {
this.Button1.Text="新增";
GetData();
}
}

public static void InsertUser(string name,string sex)
 {
SqlHelper.ExecuteNonQuery(conn, CommandType.Text, "insert into [user] values('" + name + "','" + sex + "')");
//SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "insert_user", new SqlParameter("@name", name),new SqlParameter("@sex", sex));
}

public static void UpdateUser(int ID,string name, string sex)
 {
SqlHelper.ExecuteNonQuery(conn, CommandType.Text, "update [user] set name='" + name + "',sex='" + sex + "' where id=" + ID);
}

public static SqlDataReader GetUserID(int ID)
 {
return SqlHelper.ExecuteReader(conn, CommandType.Text, "select * from [user] where id=" + ID);
}

public static void DelUser(int ID)
 {
SqlHelper.ExecuteNonQuery(conn, CommandType.Text, "delete [user] where id="+ID);
}

protected void GetData()
 {
this.GridView1.DataSource = SqlHelper.ExecuteDataset(conn,CommandType.Text,"select * from [user]").Tables[0];
this.GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
 {
if (this.Button1.Text == "更新")
 {
UpdateUser(Convert.ToInt32(this.Button1.CommandArgument),this.TextBox1.Text.Trim(), TextBox2.Text.Trim());
this.Button1.Text = "新增";
}
else
 {
InsertUser(this.TextBox1.Text.Trim(), TextBox2.Text.Trim());
}
GetData();
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
if (Convert.ToString(e.CommandName) =="set")
 {
SqlDataReader dr=GetUserID(Convert.ToInt32(e.CommandArgument));
dr.Read();
this.TextBox1.Text = dr["name"].ToString();
this.TextBox2.Text = dr["sex"].ToString();
this.Button1.CommandArgument = dr["ID"].ToString();
dr.Close();
this.Button1.Text = "更新";
}
if (Convert.ToString(e.CommandName) =="del")
 {
DelUser(Convert.ToInt32(e.CommandArgument));
GetData();
}
}
}

数据库:ABC 表:user 字段:id(自动编号,主键),name,sex

SqlHelper:


发表于
2007-12-03 03:20
期待
阅读( 1666)
评论()
收藏
举报
|
|