数据库操作实例--插入、修改、删除

2008-02-23 07:40:38来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

  新建一个网站,一个default.aspx文档,代码如下:

  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);
}
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 常见数据库分页SQL语句

下一篇: SQL Server和MySQL的安全性分析