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>
        <div style="text-align: center">
            <table border="0" cellpadding="0" cellspacing="0" style="width: 604px; height: 420px; border-left-color: #0033cc; border-bottom-color: #0033cc; border-top-style: none; border-top-color: #0033cc; border-right-style: none; border-left-style: none; border-right-color: #0033cc; border-bottom-style: none; background-color: #cccccc;">
                <tr>
                    <td colspan="3" style="height: 35px">
                        <strong><span style="font-size: 24pt; color: #ff0033">删除所有记录</span></strong></td>
                </tr>
                <tr>
                    <td colspan="3" style="height: 35px">
                        选择需要清空的表:
                        <asp:DropDownList ID="ddlTable" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlTable_SelectedIndexChanged"
                            Width="118px">
                        </asp:DropDownList>
                        &nbsp;<asp:Button ID="btnDel" runat="server" Height="23px" Text="删除所有记录" Width="87px" OnClick="btnDel_Click" /></td>
                </tr>
                <tr>
                    <td colspan="3" style="height: 290px" valign="top">
                        <asp:Label ID="Label1" runat="server" Height="21px" Text="Label"
                            Width="150px"></asp:Label><br />
                        <asp:GridView ID="gvTable" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge"
                            BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" Height="263px"
                            Width="491px">
                            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
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)
    {
        if (!IsPostBack)
        {
            ddlBind();
            gvBind();
        }
        Label1.Visible = false;
    }

    //创建数据库连接
    protected SqlConnection createCon()
    {
        string str = ConfigurationSettings.AppSettings["strCon"];
        SqlConnection con = new SqlConnection(str);
        return con;
    }
    //绑定下拉列表框
    protected void ddlBind()
    {
        SqlConnection con = createCon();
        con.Open();
        //sql语句查询出所有表的名称
        string sqlSel = " SELECT name FROM db2_Tome2.dbo.sysobjects WHERE xtype='U' AND STATUS>=0";
        SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        ddlTable.DataSource = ds.Tables[0].DefaultView;
        ddlTable.DataTextField = "name";        
        ddlTable.DataBind();
        con.Close();
    }
    //绑定gridView控件显示表中的记录
    protected void gvBind()
    {
        SqlConnection con = createCon();
        con.Open();
        string sqlSel = "select * from " + ddlTable.SelectedValue;
        SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        //判断表中数据是否为空
        if (ds.Tables[0].DefaultView.Count > 0)
        {
            gvTable.DataSource = ds.Tables[0].DefaultView;
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "该表中没有记录!";
        }
        gvTable.DataBind();
        con.Close();
    }
  
    protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvBind();
    }
    //删除表中的记录
    protected void btnDel_Click(object sender, EventArgs e)
    {
        SqlConnection con = createCon();
        con.Open();
        //sql语句删除所有记录
        string sqlDel = "delete from " + ddlTable.SelectedValue;
        SqlCommand com = new SqlCommand(sqlDel, con);
        com.ExecuteNonQuery();
        gvBind();
        con.Close();
    }
}

 

posted on 2013-03-11 05:33  松竹梅  阅读(167)  评论(0编辑  收藏  举报