📄 databaseproperties.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SqlAdmin;
namespace SqlWebAdmin
{
/// <summary>
/// Summary description for DatabaseProperties.
/// </summary>
public class DatabaseProperties : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label NamePropertyLabel;
protected System.Web.UI.WebControls.Label StatusPropertyLabel;
protected System.Web.UI.WebControls.Label OwnerPropertyLabel;
protected System.Web.UI.WebControls.Label DateCreatedPropertyLabel;
protected System.Web.UI.WebControls.Label SizePropertyLabel;
protected System.Web.UI.WebControls.Label SpaceAvailablePropertyLabel;
protected System.Web.UI.WebControls.Label NumberOfUsersPropertyLabel;
protected System.Web.UI.WebControls.Label ErrorLabel;
protected FileProperties DataFileProperties;
protected FileProperties LogFileProperties;
protected System.Web.UI.WebControls.Button ApplyButton;
protected System.Web.UI.WebControls.Button CancelButton;
public DatabaseProperties()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
HttpCookie cookie = Request.Cookies["WebDataAdministrator"];
if (cookie == null)
Response.Redirect("default.aspx?error=sessionexpired");
SqlServer server = new SqlServer(cookie.Values["server"], cookie.Values["username"], cookie.Values["password"]);
// Get database properties and fill in their info
server.Connect();
SqlDatabase database = server.Databases[Request["database"]];
if (database == null) {
server.Disconnect();
// Database doesn't exist - break out and go to error page
Response.Redirect(String.Format("error.aspx?error={0}", 1000));
return;
}
SqlDatabaseProperties props = database.GetDatabaseProperties();
server.Disconnect();
NamePropertyLabel.Text = Server.HtmlEncode(props.Name);
StatusPropertyLabel.Text = Server.HtmlEncode(props.Status);
OwnerPropertyLabel.Text = Server.HtmlEncode(props.Owner);
DateCreatedPropertyLabel.Text = Server.HtmlEncode(Convert.ToString(props.DateCreated));
SizePropertyLabel.Text = props.Size.ToString("f2");
SpaceAvailablePropertyLabel.Text = props.SpaceAvailable.ToString("f2");
NumberOfUsersPropertyLabel.Text = Convert.ToString(props.NumberOfUsers);
// On first load of the page, force data gathering...
if (!IsPostBack)
CancelButton_Click(null, null);
}
private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
}
#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ApplyButton.Click += new System.EventHandler(this.ApplyButton_Click);
this.CancelButton.Click += new System.EventHandler(this.CancelButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CancelButton_Click(object sender, System.EventArgs e)
{
ErrorLabel.Visible = false;
HttpCookie cookie = Request.Cookies["WebDataAdministrator"];
if (cookie == null)
Response.Redirect("default.aspx?error=sessionexpired");
SqlServer server = new SqlServer(cookie.Values["server"], cookie.Values["username"], cookie.Values["password"]);
// Get database properties and fill in their info
server.Connect();
SqlDatabase database = server.Databases[Request["database"]];
if (database == null) {
server.Disconnect();
// Database doesn't exist - break out and go to error page
Response.Redirect(String.Format("error.aspx?error={0}", 1000));
return;
}
SqlDatabaseProperties props = database.GetDatabaseProperties();
server.Disconnect();
DataFileProperties.Properties = props.DataFile;
LogFileProperties.Properties = props.LogFile;
}
private void ApplyButton_Click(object sender, System.EventArgs e)
{
ErrorLabel.Visible = false;
HttpCookie cookie = Request.Cookies["WebDataAdministrator"];
if (cookie == null)
Response.Redirect("default.aspx?error=sessionexpired");
SqlServer server = new SqlServer(cookie.Values["server"], cookie.Values["username"], cookie.Values["password"]);
// Grab data from the form
SqlDatabaseProperties props = null;
SqlFileProperties dataFileProperties = null;
SqlFileProperties logFileProperties = null;
try {
dataFileProperties = DataFileProperties.Properties;
}
catch (Exception ex) {
ErrorLabel.Visible = true;
ErrorLabel.Text = "Error reading data file properties: " + Server.HtmlEncode(ex.Message).Replace("\n", "<br>") + "<br><br>";
return;
}
try {
logFileProperties = LogFileProperties.Properties;
}
catch (Exception ex) {
ErrorLabel.Visible = true;
ErrorLabel.Text = "Error reading log file properties: " + Server.HtmlEncode(ex.Message).Replace("\n", "<br>") + "<br><br>";
return;
}
props = new SqlDatabaseProperties(dataFileProperties, logFileProperties);
// First validate input ourselves
ArrayList errorList = new ArrayList();
if (props.DataFile.FileGrowth < 0)
errorList.Add("Data file growth must be positive");
if (props.DataFile.MaximumSize < -1)
errorList.Add("Data file maximum size must be positive");
if (props.LogFile.FileGrowth < 0)
errorList.Add("Log file growth must be positive");
if (props.LogFile.MaximumSize < -1)
errorList.Add("Log file maximum size must be positive");
if (errorList.Count > 0) {
ErrorLabel.Visible = true;
ErrorLabel.Text = "The following error(s) occured:<br><ul>";
for (int i = 0; i < errorList.Count; i++)
ErrorLabel.Text += String.Format("<li>{0}</li>", (string)errorList[i]);
ErrorLabel.Text += "</ul>";
return;
}
server.Connect();
SqlDatabase database = server.Databases[Request["database"]];
if (database == null) {
server.Disconnect();
// Database doesn't exist - break out and go to error page
Response.Redirect(String.Format("error.aspx?error={0}", 1000));
return;
}
// Try to set properties
try {
database.SetDatabaseProperties(props);
}
catch (Exception ex) {
// Show error message and quit
server.Disconnect();
ErrorLabel.Text = "The following error occured:<br>" + Server.HtmlEncode(ex.Message).Replace("\n", "<br>") + "<br><br>";
return;
}
// Only reload data if there were no errors
// Get database properties and fill in their info
props = database.GetDatabaseProperties();
DataFileProperties.Properties = props.DataFile;
LogFileProperties.Properties = props.LogFile;
server.Disconnect();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -