using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 InstallSucceed : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && CheckConnection())
        {
            try
            {
                string path1 = HttpContext.Current.Server.MapPath("~/install/sql/CreateU.sql");
                string path2 = HttpContext.Current.Server.MapPath("~/install/sql/CreateSP.sql");
                string path3 = HttpContext.Current.Server.MapPath("~/install/sql/CreateV.sql");
                string[] strpath ={ path1, path3, path2 };
                for (int i = 0; i < strpath.Length; i++)
                {
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
                    System.IO.StreamReader reader = new System.IO.StreamReader(strpath[i], System.Text.Encoding.GetEncoding("gb2312"));
                    string st = reader.ReadToEnd();
                    SqlCommand com = new SqlCommand(st, con);
                    using (con)
                    {
                        con.Open();
                        com.ExecuteNonQuery();
                    }
                }
                InsertData();
                InsertArea();

            }
            catch(Exception ex)
            {
                this.right1.Visible = true;
                this.right.Visible = false;
                this.labError.InnerHtml = "安装出现错误请重新安装！";
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private bool CheckConnection()
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString.ToString());
        try
        {
            connection.Open();
            if (connection.State != ConnectionState.Open)
            {
                this.right1.Visible = true;
                this.right.Visible = false;
                this.labError.InnerHtml = "<font style='font-size:14px;color:#f00'>数据库连接失败，请检查服务器信息！</font>";
                return false;
            }
        }
        catch
        {
            this.right1.Visible = true;
            this.right.Visible = false;
            this.labError.InnerHtml = "<font style='font-size:14px;color:#f00'>数据库连接失败，请检查服务器信息！</font>";
            return false;
        }
        return true;
    }

    /// <summary>
    /// 
    /// </summary>
    protected void InsertData()
    {
        //插入系统初始化信息
        string mangeName = Request.QueryString["manage"].ToString();
        string password = Request.QueryString["password"].ToString();
        string webName = Request.QueryString["web"].ToString();
        string webUrl = Request.QueryString["url"].ToString();
        string Md5 = Request.QueryString["Md5"].ToString();
        string str1 = "INSERT [W_Manager] ([ManagerName],[PassWord],[RoleID],[Lockedup],[LastIP],[LastDate],[AddDate]) VALUES ( '" + mangeName + "','" + password + "',1,0,'123.175.101.219','2009-7-6 8:49:17','2009-5-24 16:57:51')";
        string str2 = "INSERT  into [W_WebBasic] ([SystemTel],[HotTel],[FaxTel],[CorporationName],[Address],[PostalCode],[Email],[EmailName],[EmailPass],[SMTP],[State],[StopReadme],[WebName],[WebURL],[Catalog],[isBlog],[isSecondURL],[isForum],[Copyright],[WebLOGO],[Filter],[Rewrite],[LoginName],[AdminUserID],[Encryption]) VALUES ( '0351-7622426','0351-7622426','0351-7622426','" + webName + "','" + webUrl + "','034000','websen_4187@sohu.com','websen_4187','websen123','smtp.sohu.com',1,'网站维护中，请稍后访问。。。','" + webName + "','" + webUrl + "','admin/',0,1,0,'dassd','/img/200907/200907050113448281.jpg','aa','.aspx','websennet',1,'"+Md5+"')";
        string[] strnum ={ str1, str2 };
        for (int i = 0; i < strnum.Length; i++)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
            SqlCommand com = new SqlCommand(strnum[i], con);
            using (con)
            {
                con.Open();
                com.ExecuteNonQuery();
            }
        }
    }

    protected void InsertArea()
    {
        //插入地区数据和主营行业数据
        string path_area = HttpContext.Current.Server.MapPath("~/install/sql/InsertArea.sql");
        string pathInsertValue_ = HttpContext.Current.Server.MapPath("~/install/sql/InsertValue.sql");
        string[] path_ai ={ path_area, pathInsertValue_ };
        for (int i = 0; i < path_ai.Length; i++)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
            System.IO.StreamReader reader = new System.IO.StreamReader(path_ai[i], System.Text.Encoding.GetEncoding("gb2312"));
            string rederString = reader.ReadToEnd();
            SqlCommand com = new SqlCommand(rederString, con);
            using (con)
            {
                con.Open();
                com.ExecuteNonQuery();
            }
        }

    }
}
