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

📄 import.aspx.cs

📁 wrox c#高级编程
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
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 ImportExport.
    /// </summary>
    public class Import : System.Web.UI.Page
    {
        protected Button ImportButton;

        protected HtmlInputFile FileUploadInput;

        protected Label ImportErrorLabel;
        protected Label ImportSuccessLabel;


        private void Page_Load(object sender, System.EventArgs e)
        {
            ImportErrorLabel.Visible = false;
            ImportSuccessLabel.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"]);
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <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.ImportButton.Click += new System.EventHandler(this.ImportButton_Click);
        }
        #endregion


        private void ImportButton_Click(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"]);

            // Grab file from post data
            HttpPostedFile file = FileUploadInput.PostedFile;

            int length = file.ContentLength;

            byte[] buff = new byte[length];
            file.InputStream.Read(buff, 0, length);

            // Convert from byte array to string
            StringBuilder qsb = new StringBuilder();
            for (int i = 0; i < length; i++)
                qsb.Append(Convert.ToChar(buff[i]));

            string q = qsb.ToString();

            if (q.Trim().Length == 0) {
                ImportErrorLabel.Visible = true;
                ImportErrorLabel.Text = "Imported file contains no data.";
                return;
            }

            try {
                // No need for connect/disconnect since Query() uses ADO.NET, not DMO
                server.Query(q);
                ImportSuccessLabel.Visible = true;
            }
            catch (SqlException ex) {
                ImportErrorLabel.Visible = true;
                ImportErrorLabel.Text = "There was an error importing the database. The status of the import is unknown.<br><br>" +
                    Server.HtmlEncode(ex.Message).Replace("\n", "<br>");
            }
        }
    }
}

⌨️ 快捷键说明

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