Default.ASPx代碼:
<%@ 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" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridVIEw1_RowDeleting">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<ASP:BoundField DataFIEld="id" HeaderText="ID編號">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<FooterStyle HorizontalAlign="Left" />
</ASP:BoundFIEld>
<ASP:BoundField DataFIEld="name" HeaderText="用戶名">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Left" />
</ASP:BoundFIEld>
<ASP:BoundField DataFIEld="pwd" HeaderText="密碼">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Left" />
</ASP:BoundFIEld>
<ASP:BoundField DataFIEld="datatime" DataFormatString="{0:yyyy年MM月dd日}" HeaderText="時間" HtmlEncode="False">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Left" />
</ASP:BoundFIEld>
<asp:HyperLinkField DataNavigateUrlFIElds="id" DataNavigateUrlFormatString="Default.ASPx?id={0}"
HeaderText="編輯" Text="編輯" >
<ControlStyle Font-Underline="False" />
</ASP:HyperLinkFIEld>
<ASP:CommandFIEld HeaderText="刪除" ShowDeleteButton="True">
<ControlStyle Font-Underline="False" />
</ASP:CommandFIEld>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</ASP:GridVIEw>
<asp:Label ID="Label1" runat="server" Width="324px"></ASP:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></ASP:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></ASP:TextBox><br />
<ASP:Button ID="Button1" runat="server" Text="插入數據" OnClick="Button1_Click" />
<ASP:Button ID="Button2" runat="server" Text="修改數據" OnClick="Button2_Click" /></div>
</form>
</body>
</Html>
Default.ASPx.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 System.Data.SqlClIEnt;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from tb_Info", con);
DataSet ds = new DataSet();
adapter.Fill(ds, "tb_Info");
GridVIEw1.DataSource = ds;
GridVIEw1.DataKeyNames = new string[] { "id" };
GridVIEw1.DataBind();
con.Close();
if (Page.IsPostBack == false)
{
if (Request.QueryString["id"] == null)
{
Label1.Visible = false;
}
else
{
SqlConnection mycon = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;DataBase=snear");
mycon.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_Info where id=" + Request.QueryString["id"], mycon);
DataSet myds = new DataSet();
ada.Fill(myds, "tb_Info");
DataRowView drv = myds.Tables["tb_Info"].DefaultVIEw[0];
TextBox1.Text = drv["name"].ToString();
TextBox2.Text = drv["pwd"].ToString();
Label1.Visible = true;
Label1.Text = "您當前要修改表中的第[" + Request.QueryString["id"] + "]條記錄!";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
con.Open();
SqlCommand com = new SqlCommand("insert into tb_Info(name,pwd) values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
com.ExecuteNonQuery();
con.Close();
Page_Load(sender, e);
}
protected void GridView1_PageIndexChanging(object sender, GridVIEwPageEventArgs e)
{
this.GridVIEw1.PageIndex = e.NewPageIndex;
this.GridVIEw1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "" && TextBox2.Text != "")
{
SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
con.Open();
string Sqlstr = "update tb_Info set name='" + TextBox1.Text + "',pwd='" + TextBox2.Text + "' where id=" + Request.QueryString["id"];
SqlCommand com = new SqlCommand(Sqlstr, con);
com.ExecuteNonQuery();
con.Close();
Label1.Visible = false;
TextBox1.Text = "";
TextBox2.Text = "";
Page_Load(sender, e);
Page_Load(sender, e);
}
}
protected void GridView1_RowDeleting(object sender, GridVIEwDeleteEventArgs e)
{
SqlConnection con = new SqlConnection("Server=(local);User Id=sa;Pwd=860712;Database=snear");
con.Open();
SqlCommand com = new SqlCommand("delete from tb_Info where id='" + GridVIEw1.DataKeys[e.RowIndex].Value + "'", con);
com.ExecuteNonQuery();
con.Close();
Page_Load(sender, e);
}
}