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;
using SqlOperate;
using Base;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Threading;
public partial class Default2: System.Web.UI.Page
{
    private static string adminP = "";
    private static string dataP = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string path = HttpContext.Current.Server.MapPath("~/");
            File.Delete(path + "js/Advertisement/File");
            string strPath = "http://" + this.Request.Url.Host + "/";
            this.txt_InternetAraeName.Value = strPath;
        }
    }

    /// <summary>
    /// MD5 16位加密
    /// </summary>
    /// <param name="STR">要加密的字符串</param>
    /// <returns></returns>  
    public string Encryption(string STR, string Md5)
    {
        string password = "";
        STR = STR + Md5;
        //pwd为加密结果
        MD5 ps = MD5.Create();
        byte[] s = ps.ComputeHash(Encoding.UTF8.GetBytes(STR));
        //这里的UTF8是编码方式，你可以采用你喜欢的方式进行，比如UNcode等等
        for (int i = 0; i < s.Length; i++)
        {
            password = password + s[i].ToString();
        }
        return password;
    }

    string str = "";//数据库连接串 
    protected void Button1_Click(object sender, EventArgs e)
    {
        //创建数据库表、存储过程、视图
        string server = this.txt_server.Value.ToString().Trim();
        string name = this.txt_name.Value.ToString().Trim();
        string password = this.txt_password.Value.ToString().Trim();
        string datasql = this.txt_sql.Value.ToString().Trim();
        string Md5 = this.txt_Md5.Value.ToString().Trim();
        string AdminPswd = this.txt_password1.Value.ToString().Trim();
        string AdminMd5 = Encryption(AdminPswd, Md5);
        string IsImport="false";
        if (cbxImportData.Checked == true)
            IsImport = "true";
        str = "Data Source=" + server + "; Initial Catalog=" + datasql + "; User ID=" + name + "; Password=" + password;
        try
        {
            if (ReadXml(server, name, password, datasql))             //为修改配置文件中数据库连接串的方法
            {
                Response.Redirect("InstallSucceed.aspx?manage=" + this.txt_memberNumber.Value.ToString().Trim() + "&password=" + AdminMd5 + "&web=" + this.txt_InternetName.Value.ToString().Trim() + "&url=" + this.txt_InternetAraeName.Value.ToString().Trim() + "&Md5=" + Md5+"&IsImport="+IsImport);
            }
        }
        catch (Exception ex)
        {
            this.main_box.Visible = false;
            this.divError.InnerHtml = "程序出现问题，请重新安装";
        }
    }

    /// <summary>
    /// 修改配置文件中的连接字符串
    /// </summary>
    /// <param name="server">服务器名称</param>
    /// <param name="userid">用户ID</param>
    /// <param name="password">密码</param>
    /// <param name="database">数据库名称</param>
    protected bool ReadXml(string server, string userid, string password, string database)
    {
        try
        {
            string sqlconstring = "Data Source=" + server + "; User ID=" + userid + "; Password=" + password + "; Initial Catalog=" + database;
            string path = HttpContext.Current.Server.MapPath("~/web.Config");
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(path);
            System.Xml.XmlNode node = doc.SelectSingleNode("configuration/connectionStrings/add");
            System.Xml.XmlElement eml = (System.Xml.XmlElement)node;
            eml.SetAttribute("connectionString", sqlconstring);
            doc.Save(path);
            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
}
