⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createtable.aspx.cs

📁 wrox c#高级编程
💻 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 CreateTable.
    /// </summary>
    public class CreateTable : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TableNameTextBox;
        protected System.Web.UI.WebControls.Button CreateNewTableButton;
        protected System.Web.UI.WebControls.RequiredFieldValidator TableNameRequiredValidator;
        protected System.Web.UI.WebControls.Label ErrorCreatingLabel;
    
        public CreateTable()
        {
            Page.Init += new System.EventHandler(Page_Init);
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            ErrorCreatingLabel.Visible = false;
        }

        private void CreateNewTableButton_Click(object sender, System.EventArgs e)
        {
            if (TableNameTextBox.Text.Length == 0) 
            {
                ErrorCreatingLabel.Visible = true;
                ErrorCreatingLabel.Text = "The new table name cannot be blank";
                return;
            }

            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"]);

            string databaseName = Request["database"];

            ErrorCreatingLabel.Visible = false;

            server.Connect();
            SqlTable table = server.Databases[databaseName].Tables[TableNameTextBox.Text];

            // Ensure that the table doesn't exist yet
            if (table == null) 
            {
                // Now we have to do a quick check and see if it's a valid name for a table
                // The only reliable way to do this is to try to create the table and see what happens

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

                // In order to find out whether the table name is valid, we create a temporary dummy table
                // and see what happens.
                SqlTable dummyTable = null;

                try 
                {
                    dummyTable = database.Tables.Add(TableNameTextBox.Text, new SqlColumnInformation[] {new SqlColumnInformation("Column1")});
                }
                catch (Exception ex) 
                {
                    // Disconnect and show error
                    if (dummyTable != null)
                        dummyTable.Remove();

                    server.Disconnect();
                    ErrorCreatingLabel.Visible = true;
                    ErrorCreatingLabel.Text = "There was an error creating the table:<br>" + Server.HtmlEncode(ex.Message).Replace("\n", "<br>");
                    return;
                }

                // Delete the dummy table
                dummyTable.Remove();


                server.Disconnect();
                Response.Redirect(String.Format("editcolumn.aspx?database={0}&table={1}", Server.UrlEncode(databaseName), Server.UrlEncode(TableNameTextBox.Text)));
            }
            else 
            {
                server.Disconnect();
                ErrorCreatingLabel.Visible = true;
                ErrorCreatingLabel.Text = "A table with this name already exists.";
            }
        }

        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.Load += new System.EventHandler(this.Page_Load);
            this.CreateNewTableButton.Click += new System.EventHandler(this.CreateNewTableButton_Click);

        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -