using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
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 System.Diagnostics;
using Fm.Bussiness;
using System.Xml;

public partial class install_stall_1 : System.Web.UI.Page
{
    //实例化文件操作类
   ATL.IO.IOStream stream = new ATL.IO.IOStream();
    //公用SQL脚本执行类
    public_select ps = new public_select();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            TextBox9.Text = Request.Url.ToString().Replace("install/stall_1.aspx", "");
            tab_end.Visible = false;
        }
        if (Request.Form["server"] != null)
        {
            Response.ContentType = "text/html";
            string con = "server=" + Request.Form["server"].ToString() + ";uid=" + Request.Form["uid"].ToString() + ";pwd=" + Request.Form["pwd"].ToString() + ";database=" + Request.Form["database"].ToString() + "";
            Response.Write(GetCon(con));

            Response.Flush();
            Response.Close();
        }
    }
    string GetCon(string str)
    {
        string bo="";
        SqlConnection con = new SqlConnection(str);
        try
        {
            con.Open();
            if (con.State == 0)
            {
                bo = "0";
            }
            else
            {
                bo = "1";
            }
        }
        finally 
        {
            con.Close();
        }
        return bo;
        
    }
    //保存设置的数据
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //修改配置文件链接参数
        ConfigSave();
        //创建表.视图.存储过程
        CreateTable();
        //填充基本数据
        CreateData();
       
        //跳转到成功安装页面
        tab_start.Visible = false;
        tab_end.Visible = true;
      
        
    }
    //填充基本的数据
    void CreateData()
    {
        //基本数据填充
        string data=stream.ReadFile(Server.MapPath("data/data.sql"));
        ExecuteNonQuery(data);
        //添加用户数据
        //将获取的密码进行加密
        string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox6.Text.Trim(), "MD5");
        //调用用户添加类
        sys_user_user sys = new sys_user_user();
        if (ExecuteNonQuery("insert into Fm_user values('" + TextBox5.Text.Trim() + "','" + pass + "','创始人','"+TextBox7.Text.Trim()+"')"))
        { 
            //把用户数据关联到权限组
            //DataView dv = ps.Getps("Fm_user","top 1 u_id","u_id<>0 order by u_id desc");
            ExecuteNonQuery("insert into Fm_urcenter values(1,1)");
        }
        //网站基本设置的数据
        ExecuteNonQuery("insert into Fm_webset values(1,'" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','upload','飞梦新闻文章系统','飞梦-从简单开始-到简单结束','飞梦版权所有','" + TextBox11.Text + "',1,1,'html/')");

    }
    //创建表.视图.存储过程
    void CreateTable()
    {
      
        //获取创建表,视图 的SQL 脚本内容
        string table = stream.ReadFile(Server.MapPath("data/table_view.sql")).Replace("GO","|");
        string[] sqlstr = table.Split('|');
        for (int i = 0; i < sqlstr.Length; i++)
        {
            ExecuteNonQuery(sqlstr[i]);
           
        }
        //获取创建存储过程的脚本内容
        string proc = stream.ReadFile(Server.MapPath("data/proc.sql")).Replace("GO", "|");
        //循环执行分割的SQL语句
        string[] p = proc.Split('|');
        for (int k = 0; k < p.Length; k++)
        {
            ExecuteNonQuery(p[k]);
           
        }
      

    }
    //修改配置文件数据库连接字符串
    void ConfigSave()
    {
        //新的连接字符串
        string con = "server=" + TextBox1.Text + ";uid=" + TextBox2.Text + ";pwd=" + TextBox3.Text + ";database=" + TextBox4.Text + "";
        //实例化XML操作类
        XmlDataDocument doc = new XmlDataDocument();
        //加载XML 文件
        string xmlpath = Server.MapPath("../web.config");
        doc.Load(xmlpath);

        //获取根节点
        XmlNode root =null;
        foreach (XmlNode nd in doc.ChildNodes)
        {
            if (nd.Name == "configuration")
            {
                root = nd;
            }
        }
        //循环判断数据库连接字符节点
        foreach (XmlNode node in root.ChildNodes)
        {
            
            if (node.Name == "connectionStrings")
            {
               
                foreach (XmlNode child in node.ChildNodes)
                {
                    if (child.Attributes["name"].Value == "Conn")
                    {
                        child.Attributes["connectionString"].Value = con;
                       
                    }
                }
            }
        }
        //保存修改的值
        doc.Save(xmlpath);
        
       
    }
    bool ExecuteNonQuery(string sql)
    {
        SqlConnection conn = new SqlConnection("server="+TextBox1.Text+";uid="+TextBox2.Text+";pwd="+TextBox3.Text+";database="+TextBox4.Text+"");
        using (SqlCommand cmd = new SqlCommand())
        {
            
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
    }
 
}
