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 id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="text-align: center">
            <table id="TABLE1" border="0" cellpadding="0" cellspacing="0" style="width: 298px; height: 88px; background-color: #a7f1ff;">
                <tr>
                    <td colspan="3" style="height: 24px">
                        <span style="font-size: 16pt; color: #ff0000"><strong>收缩数据库</strong></span></td>
                    <td colspan="1" style="height: 24px">
                    </td>
                </tr>
                <tr>
                    <td style="width: 94px">
                    </td>
                    <td style="width: 282px">
                        选择数据库:</td>
                    <td style="width: 215px; text-align: left;">
                        <asp:DropDownList ID="ddlDataName" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlDataName_SelectedIndexChanged">
                        </asp:DropDownList></td>
                    <td style="width: 100px">
                    </td>
                </tr>
                <tr>
                    <td style="width: 94px; height: 28px">
                    </td>
                    <td style="width: 282px; height: 28px;">
                        当前大小:</td>
                    <td style="width: 215px; height: 28px; text-align: left;">
        <asp:Label ID="labDataSize" runat="server" Text="Label" Visible="False" ForeColor="Maroon"></asp:Label></td>
                    <td style="width: 100px; height: 28px">
                    </td>
                </tr>
                <tr>
                    <td style="width: 94px">
                    </td>
                    <td style="width: 282px">
                        收缩后大小:</td>
                    <td style="width: 215px; text-align: left;">
                        <asp:Label ID="labShrinkSize" runat="server" Text="Label" Visible="False" ForeColor="Red"></asp:Label></td>
                    <td style="width: 100px">
                    </td>
                </tr>
                <tr>
                    <td colspan="1" style="width: 94px">
                    </td>
                    <td colspan="2">
                        <asp:Button ID="btnShrink" runat="server" OnClick="btnShrink_Click" Text="收缩数据库" /></td>
                    <td colspan="1">
                    </td>
                </tr>
            </table>
        </div>
    
    </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)
    {
        if (!IsPostBack)
        {
            ddlBind();   //自定义方法绑定下拉列表框
            labDataSize.Visible = true;     //显示label控件
            //显示当前数据库的大小
            labDataSize.Text = getSize(ddlDataName.SelectedValue);
        }
    }
    //创建数据库连接
    protected SqlConnection createCon()
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        return con;
    }
    //自定义方法将所有数据库名称绑定到下拉列表框中
    protected void ddlBind()
    {
        SqlConnection con = createCon(); ;
        con.Open(); //打开数据库连接
        //sql语句查询所有数据库信息
        string str = "exec sp_helpdb";
        SqlDataAdapter sda = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);       //填充数据集
        ddlDataName.DataSource = ds.Tables[0].DefaultView;
        ddlDataName.DataTextField = "name"; //设置下拉列表显示的文本
        ddlDataName.DataBind();
        con.Close();
    }
    //自定义方法返回当前数据库的大小
    protected string getSize(string dataName)
    {
        SqlConnection con = createCon(); ;
        con.Open();
        //sql语句获取指定数据库的信息
        string str = "exec sp_helpdb " + dataName;
        SqlCommand com = new SqlCommand(str, con);
        SqlDataReader sdr = com.ExecuteReader();
        sdr.Read(); //读取一条记录
        //获取该数据库的大小
        string size = sdr["db_size"].ToString();
        sdr.Close();
        con.Close();
        return size;

    }

    //执行数据库收缩操作
    protected void btnShrink_Click(object sender, EventArgs e)
    {
        string str = "DBCC SHRINKDATABASE ('" + ddlDataName.SelectedValue + "')";
        SqlConnection con = createCon();
        con.Open();     //打开数据库连接
        SqlCommand com = new SqlCommand(str, con);
        try
        {
            com.ExecuteNonQuery();  //执行sql语句
            labShrinkSize.Visible = true;   //显示label控件
            //显示当前数据库收缩后的大小
            labShrinkSize.Text = getSize(ddlDataName.SelectedValue);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }
    //通过Label控件显示当前数据库的大小
    protected void ddlDataName_SelectedIndexChanged(object sender, EventArgs e)
    {
        labShrinkSize.Visible = false;
        labDataSize.Visible = true;
        //显示当前数据库的大小
        labDataSize.Text = getSize(ddlDataName.SelectedValue);

    }
}

 

posted on 2013-03-19 19:17  松竹梅  阅读(270)  评论(0编辑  收藏  举报