<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Publics"%>
<%@ Import Namespace="Kesion.HtmlTags"%>
<!DOCTYPE html>
<html>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <script runat="server">
         static string connStr=string.Empty;
         static StringBuilder ReportTxt=new StringBuilder();
         protected void Page_Load(object sender, EventArgs e)
         {
             InnerHtml("正在升级数据库结构...");
             AddColumn("KS_InfoList", "GoodNum", "int default 0");
             AddColumn("KS_InfoList", "BadNum", "int default 0");
             AddColumn("KS_InfoList", "FavNum", "int default 0");
             AddColumn("KS_InfoList", "ShareNum", "int default 0");
             AddColumn("KS_ProductList", "GoodNum", "int default 0");
             AddColumn("KS_ProductList", "BadNum", "int default 0");
             AddColumn("KS_ProductList", "FavNum", "int default 0");
             AddColumn("KS_ProductList", "ShareNum", "int default 0");
             DataFactory.ExecuteNonQuery("Update KS_InfoList Set GoodNum=0,BadNum=0,FavNum=0,ShareNum=0");
             DataFactory.ExecuteNonQuery("Update KS_ProductList Set GoodNum=0,BadNum=0,FavNum=0,ShareNum=0");
             using (DataTable dt = DataFactory.GetDataTable("select * from KS_Model"))
             {
                 if (dt.Rows.Count > 0)
                 {
                     foreach (DataRow dr in dt.Rows)
                     {
                         AddColumn(dr["ModelTable"].ToString(), "GoodNum", "int default 0");
                         AddColumn(dr["ModelTable"].ToString(), "BadNum", "int default 0");
                         AddColumn(dr["ModelTable"].ToString(), "FavNum", "int default 0");
                         AddColumn(dr["ModelTable"].ToString(), "ShareNum", "int default 0");
                         DataFactory.ExecuteNonQuery("Update "+dr["ModelTable"].ToString()+" Set GoodNum=0,BadNum=0,FavNum=0,ShareNum=0");

                     }
                 }
             }
             
             DelFile("/bin/Kesion.XML.dll");
             DelFile("/bin/Kesion.Lucene.DLL");
             DelFile("/bin/Kesion.Components.dll");
             DelFile("/bin/Kesion.Config.dll");
             DelFile("/bin/Kesion.UnionPay.Dll");
             DelFile("/bin/Kesion.WeiPay.dll");
             DelFile("/bin/Kesion.Collection.dll");
             DelFile("/bin/Kesion.RefreshHTML.dll");
             DelFile("/bin/Kesion.DataBase.dll");
             DelFile("/bin/Kesion.Module.dll");
             DelFile("/bin/Kesion.Controls.dll");
             DelFile("/bin/Kesion.HtmlTags.dll");
            

            #region 升级存储过程
            string[] files;
            files = Directory.GetFiles(Server.MapPath("procedure"));
            foreach (string file in files)
            {
                System.IO.FileInfo fileInfo = new FileInfo(file);
                try
                {
                    DataFactory.ExecuteNonQuery("drop proc " + fileInfo.Name.Replace(".txt", string.Empty)); //删除存储过程
                }
                catch (Exception ex)
                {
                    InnerHtml("删除存储过程[" + fileInfo.Name.Replace(".txt", string.Empty) + "]失败，原因：" + ex.Message);
                }
                try
                {
                    string procTxt = File.ReadAllText(Server.MapPath("procedure/" + fileInfo.Name));
                    DataFactory.ExecuteNonQuery(procTxt);  //创建存储过程.
                }
                catch (Exception ex)
                {
                    InnerHtml("创建存储过程[" + fileInfo.Name.Replace(".txt", string.Empty) + "]失败，原因：" + ex.Message);
                }
            }
            

            #endregion

             InnerHtml("恭喜，数据库结构升级成功!");
         }
         /// <summary>
         /// 添加字段
         /// </summary>
         /// <param name="TableName">数据表名</param>
         /// <param name="ColumnName">字段名</param>
         /// <param name="ColumnType">数据表类型</param>
         public void AddColumn(string TableName, string ColumnName, string ColumnType)
         {
             try
             {
                 DataFactory.ExecuteNonQuery(string.Format("Alter Table {0} Add {1} {2}", TableName, ColumnName, ColumnType));
                 InnerHtml("新建 " + TableName + " 表中字段 " + ColumnName + " 成功!");
             }
             catch (Exception ex)
             {
                 if (ex.Message.IndexOf("中多次指定了列名") != -1 && ex.Message.IndexOf("已经存在于表") != -1)
                 {
                     InnerHtml("新建 " + TableName + " 表中字段<font color='blue'>错误</font>，请手动将数据库中 <B>" + ColumnName + "</B> 字段建立，属性为 <B>" + ColumnType + "</B>，原因" + ex.Message);
                 }
             }

         }
         /// <summary>
         /// 删除字段
         /// </summary>
         /// <param name="TableName"></param>
         /// <param name="ColumnName"></param>
         public void DelColumn(string TableName, string ColumnName)
         {
             try
             {
                 DataFactory.ExecuteNonQuery(string.Format("Alter Table {0} Drop COLUMN {1}", TableName, ColumnName));
                 InnerHtml("删除 " + TableName + " 表中字段 " + ColumnName + " 成功!");
             }
             catch (Exception ex)
             {
                 if (ex.Message.IndexOf("不存在") == -1)
                 {
                     InnerHtml("删除 " + TableName + " 表中字段<font color='blue'>错误</font>，请手动将数据库中 <B>" + ColumnName + "</B> 字段删除，原因" + ex.Message);
                 }
             }


         }
         /// <summary>
         /// 修改字段
         /// </summary>
         /// <param name="TableName"></param>
         /// <param name="ColumnName"></param>
         /// <param name="ColumnType"></param>
         public void ModColumn(string TableName, string ColumnName, string ColumnType)
         {
             try
             {
                 DataFactory.ExecuteNonQuery(string.Format("Alter Table {0} Alter Column {1} {2}", TableName, ColumnName, ColumnType));
                 InnerHtml("更改 " + TableName + " 表中字段属性 " + ColumnName + " 成功!");
             }
             catch (Exception ex)
             {
                 InnerHtml("更改 " + TableName + " 表中字段属性<font color='blue'>错误</font>，请手动将数据库中 <B>" + ColumnName + "</B> 字段更改为 <B>" + ColumnType + "</B> 属性，原因" + ex.Message);
             }
         }
         //JS InnerHtml （追加信息）
         public void InnerHtml(string msg)
         {
             Response.Write(string.Format(@"<li>{0}</li>", msg));
             Response.Flush();
         } 

              /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filename">文件名及路径，如/uploadfiles/1.gif</param>
        public static void DelFile(string filename)
        {
            #region 删除文件
            try
            {
                FileInfo fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filename));
                if (fi.Exists)
                {
                 
                    fi.Attributes = FileAttributes.Normal;
                    fi.Delete(); 
                }
            }
            catch(Exception ex)
            {
               
            }
            #endregion
        }
        </script>
    </div>
    </form>
</body>
</html>
