点击回首页
我的浏览记录 | | 帮助?
当前位置:
首页>> 论坛社区>> 动网论坛DotNet版1.0源码(VS2005)>> 源文件浏览
[免费版 Free] WebForm,下载次数:6234 次 | 关键字: 持久层 动网 论坛 ORM框架多层开发

源码截图

源码目录树

当前路径:WebApp/upgrade/upgrade.1.0.0.3000.aspx     <%@ Page Src="UpgradeBase.cs" Inherits="Dvbbs.Upgrade.UpgradeBase" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="Dvbbs.Utils" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="Dvbbs.bbs.Entity" %>
<%@ Import Namespace="Dvbbs.bbs.Handlers" %>
<%@ Import Namespace="Dvbbs.Data" %>
<script language="C#" runat="server">

protected override void OnInit(EventArgs e)
{
	Response.ContentType = "text/html";
	Response.Charset = "gb2312";

	jsonReply.Ver = "1.0.0.3000";
	jsonReply.FileName ="upgrade.1.0.0.3000.aspx";
	jsonReply.RetVal = 1;
	jsonReply.ErrorMsg = "";
	jsonReply.Args = "";
	jsonReply.Steps = 0;

	try
	{
		string isclose=config["ServiceDown"].ToString();
		if(isclose=="False")
		{	
			jsonReply.RetVal = 0;
			jsonReply.ErrorMsg = "当前论坛未关闭,请先关闭论坛!";
			return;
		}
		DoUpgrade();
		
		if(jsonReply.RetVal == 1)			
			UpdateVersion(); // 更新版本号
		
		Response.Write(jsonReply.ToJSONString());		
	}
	finally
	{
		base.Finish();
	}
	Response.End();	
}
private void DoUpgrade()
{
	string AccessTimes = Fetch.Get("AccessTimes");
	if(AccessTimes=="" || AccessTimes==null)
	{
		SaveUpgradeLog("******开始升级:VER(" + jsonReply.Ver + ")" + System.DateTime.Now.ToLongDateString() + " " + System.DateTime.Now.ToLongTimeString() + "******");
		if(!UpdateDataTableV1_0_0_3000_rc4())
			return;
	}
	string current_proc = Fetch.Get("current_proc");
	if(current_proc=="0" || current_proc=="" || current_proc ==null)
	{
		if(!ReSetUserTableV1_0_0_3000_rc4())
			return;
	}	
	SaveUpgradeLog("******OK,升级结束:" + System.DateTime.Now.ToLongDateString() + " " + System.DateTime.Now.ToLongTimeString() + "******");
	
}
private bool ReSetUserTableV1_0_0_3000_rc4()
{
	try
	{
		SaveUpgradeLog("开始修改用户表权限:");
		uint start_userid = (0 == Fetch.Get("start_userid").Length ? 1 : uint.Parse(Fetch.Get("start_userid")));
		uint last_userid = ReSetUserTable1(conn, start_userid, 100);
		if(last_userid > 0)
		{
			jsonReply.RetVal = 2;
			jsonReply.Args = "current_proc=0&AccessTimes=1&start_userid=" + (last_userid + 1).ToString();
			SaveUpgradeLog("用户ID:"+last_userid);
		}
		else
		{
			jsonReply.RetVal = 1;		
		}
		SaveUpgradeLog("修改用户表权限成功!");
		return true;

	}
	catch(Exception ex)
	{
		jsonReply.ErrorMsg = "修改用户表权限错误:" + ex.Message;
		jsonReply.RetVal = 0;
		SaveUpgradeLog("ERROR-->ReSetUserTableV1_0_0_3000_rc4():" + jsonReply.ErrorMsg);
		return false;
	}
}
private uint ReSetUserTable1(Connection conn, uint startUserId, ushort pageSize)
{
	// 参数检测
	if (null == conn)
	{
		throw new ArgumentNullException("conn");
	}
	if (0 == startUserId)
	{
		throw new ArgumentException("startTopicId的值必须大于0!");
	}
	if (0 == pageSize)
	{
		throw new ArgumentException("pageSize的值必须大于0!");
	}

	int last_UserId = 0;

	// 获取以逗号分隔的 UserId 的列表
	StringBuilder sb = new StringBuilder("0");
	using (IDataReader reader = conn.GetReader("SELECT TOP " + pageSize.ToString() + " userid FROM " + Fetch.TablePrefix + "user WHERE userid>="+ startUserId.ToString()+" order by userid asc"))
	{
		while (reader.Read())
		{
			last_UserId = reader.GetInt32(0);
			sb.Append("," + last_UserId.ToString());
		}
	}
	// 如果 sb.Length 等于 1,说明没有找到更多的记录,此时应返回0
	if (1 == sb.Length)
	{
		return 0;
	}
	// 以逗号分隔的 UserId 的列表
	string ids = sb.ToString();

	// 释放对象 sb
	sb = null;
	try
	{		
		if (conn.Provider == DbProviderEnum.SqlServer)
		{
			conn.ExecuteNonQuery(Text.JoinString("Update ", Fetch.TablePrefix, "user set lastposttime=getdate() WHERE userid IN(", ids, ") and lastposttime is null"));
		}
		else
		{
			conn.ExecuteNonQuery(Text.JoinString("Update ", Fetch.TablePrefix, "user set lastposttime=now() WHERE userid IN(", ids, ") and lastposttime is null"));
		}
	}
	catch(Exception ex)
	{
		SaveUpgradeLog("ERROR-->ReSetUserTableV1_0_0_3000_rc4():" + jsonReply.ErrorMsg);
	}
	return (uint)last_UserId;

}
private bool UpdateDataTableV1_0_0_3000_rc4()
{
	// 更新数据库表		
	string sqlfile = "";
	string SqlPath = "upgrade\\sql\\";
	//string[] arr = null;	
	XmlTextReader reader = null;
	if (conn.Provider == DbProviderEnum.Access)
		sqlfile = Fetch.MapPath(SqlPath+"1.0.0.0.3000.rc4.ac.xml");
	else //if(conn.Provider == DbProviderEnum.SqlServer)
		sqlfile = Fetch.MapPath(SqlPath+"1.0.0.0.3000.rc4.ms.xml");
		
	try
	{
		reader = new XmlTextReader(Fetch.MapPath(sqlfile));
		reader.WhitespaceHandling = WhitespaceHandling.None;
		reader.MoveToContent();
		//StreamReader reader = new StreamReader(sqlfile, System.Text.Encoding.GetEncoding("GB2312"));
		//arr = reader.ReadToEnd().Split('~');
	}
	catch(Exception ex)
	{
		reader.Close();
		reader = null;
		jsonReply.ErrorMsg = "打开数据库升级SQL文件错误:" + ex.Message;
		jsonReply.RetVal = 0;
		SaveUpgradeLog("ERROR-->UpdateDataTableV1_0_0_3000_rc4():" + jsonReply.ErrorMsg);
		return false;
	}
	SaveUpgradeLog("开始升级数据库:");		
	OleDbConnection conn1 = null;
	
	try
	{
		if(conn.Provider == DbProviderEnum.SqlServer)                        //升级MSSQL数据库
		{
			try
			{
				while (reader.Read())
				{
					string columns = string.Empty;
					string table = string.Empty;
					string procedure = string.Empty;
					string sql = string.Empty;
					string Operate = string.Empty;
					if(0!=reader.Depth && 1!=reader.Depth)
					{
						if(2==reader.Depth && reader.IsStartElement() && "TableName" == reader.Name)
						{
							table = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "ProcedureName" == reader.Name)
						{
							procedure = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "ColumnName" == reader.Name)
						{
							columns = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "Operate" == reader.Name)
						{
							Operate = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "SqlString" == reader.Name)
						{
							sql = reader.ReadString();
						}
					}
					if(sql != "" || procedure!="")
					{
						if(0==sql.Trim().Length)
						{
							continue;
						}
						string stmt = sql.Trim().Replace("dv_",Fetch.TablePrefix);
						if(procedure!="" && Operate.Trim().ToLower() == "drop")
						{
							try
							{
								conn.Execute(stmt);
								SaveUpgradeLog("Upgrade Database: " + stmt);
							}
							catch
							{
							}
						}
						else if(Operate.Trim().ToLower() == "dropconstraint")
						{
							try
							{
								if(stmt.IndexOf("~")!=-1)
								{
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									for(int i = 0;i<arr.Length;i++)
									{
										conn.Execute(arr[i].Replace("\r\n",""));
										SaveUpgradeLog("Upgrade Database: " + stmt);
									}
								}
								else
								{
									conn.Execute(stmt);
									SaveUpgradeLog("Upgrade Database: " + stmt);
								}
							}
							catch 
							{
							}
						}
						else if (Operate.Trim().ToLower() == "delete")
						{
							try
							{
								conn.Execute(stmt);
								SaveUpgradeLog("Upgrade Database: " + stmt);
							}
							catch 
							{
							}

						}
						else if (Operate.Trim().ToLower() == "create")
						{
							try
							{
								if(stmt.IndexOf("~")!=-1)
								{
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									for(int i = 0;i<arr.Length;i++)
									{
										conn.Execute(arr[i].Replace("\r\n",""));
										SaveUpgradeLog("Upgrade Database: " + stmt);
									}
								}
								else
								{
									conn.Execute(stmt);
									SaveUpgradeLog("Upgrade Database: " + stmt);
								}
							}
							catch
							{
							}
						}
						else if(Operate.Trim().ToLower() == "update")
						{
							try
							{								
								if(sql.IndexOf("~") !=-1)
								{
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);

									for(int i = 0;i<arr.Length;i++)
									{														
											conn.Execute(arr[i].Replace("\r\n",""));
											SaveUpgradeLog("Upgrade Database: " + stmt);
										
									}									

								}
								else
								{
									conn.Execute(stmt);
									SaveUpgradeLog("Upgrade Database: " + stmt);
								}
							}
							catch
							{
							}
						}
						else if(Operate.Trim().ToLower() == "alter")
						{
							string sql111 = "";
							try
							{
								if(sql.IndexOf("~") !=-1)
								{
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									for(int i = 0;i<arr.Length;i++)
									{
										conn.Execute(arr[i].Replace("\r\n",""));
										SaveUpgradeLog("Upgrade Database: " + stmt);
									}
								}
								else
								{
										conn.Execute(stmt);
										SaveUpgradeLog("Upgrade Database: " + stmt);
								}
							}
							catch(Exception ex)
							{
								
							}
						}
						if(Operate.Trim().ToLower() == "insert")
						{
							try
							{								
								if(sql.IndexOf("~") !=-1)
								{
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									string tabHelp = "dv_help";
									if(table.Replace("dv_",Fetch.TablePrefix) == tabHelp.Replace("dv_",Fetch.TablePrefix))
									{	
										if((int)conn.GetScalar("select count(1) from "+ table.Replace("dv_",Fetch.TablePrefix) + " where H_ParentID=1 and H_Title='UBB语法'")==0)
										{
											for(int i = 0;i<arr.Length;i++)
											{

												conn.Execute(arr[i].Replace("\r\n",""));
												SaveUpgradeLog("Upgrade Database: " + stmt);
											}
										}
									}
									else
									{
										if((int)conn.GetScalar("select count(1) from "+table.Replace("dv_",Fetch.TablePrefix))==0)
										{
											for(int i = 0;i<arr.Length;i++)
											{
												
												conn.Execute(arr[i].Replace("\r\n",""));
												SaveUpgradeLog("Upgrade Database: " + stmt);
											}
										}
									}

								}
								else
								{
									conn.Execute(stmt);
									SaveUpgradeLog("Upgrade Database: " + stmt);									
								
								}
							}
							catch
							{
							}
						}	
					}
				}
			}
			catch
			{
			}
		}
		else if(conn.Provider == DbProviderEnum.Access)                        //升级Access数据库
		{
			try
			{
				while (reader.Read())
				{
					//读取数据
					string columns = string.Empty;           
					string table = string.Empty;
					string procedure = string.Empty;
					string sql = string.Empty;
					string Operate = string.Empty;
					if(0!=reader.Depth && 1!=reader.Depth)
					{
						if(2==reader.Depth && reader.IsStartElement() && "TableName" == reader.Name)
						{
							table = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "ProcedureName" == reader.Name)
						{
							procedure = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "ColumnName" == reader.Name)
						{
							columns = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "Operate" == reader.Name)
						{
							Operate = reader.ReadString().Trim().ToLower();
						}
						reader.Read();
						if(2==reader.Depth && reader.IsStartElement() && "SqlString" == reader.Name)
						{
							sql = reader.ReadString();
						}
					}
					if(sql != "")
					{
						if(0==sql.Trim().Length)
						{
							continue;
						}
						string stmt = sql.Trim().Replace("dv_",Fetch.TablePrefix);
						if (Operate.Trim().ToLower() == "drop")
						{
							try
							{
								conn.Execute(stmt);
								SaveUpgradeLog("Upgrade Database: " + stmt);
							}
							catch
							{
							}

						}
						if (Operate.Trim().ToLower() == "delete")
						{
							try
							{
								conn.Execute(stmt);
								SaveUpgradeLog("Upgrade Database: " + stmt);
							}
							catch
							{
							}

						}
						else if (Operate.Trim().ToLower() == "create")
						{
							try
							{
								conn1 = new OleDbConnection(conn.DBLink);                     //判断该表是否存在,不存在则创建
								conn1.Open();
								DataTable schemaTable = conn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
																					new object[] {null, null, null, "TABLE"});
								bool isTableExist = false;
								for(int i=0;i<schemaTable.Rows.Count;i++)
								{
									if(table.Replace("dv_",Fetch.TablePrefix) == schemaTable.Rows[i][2].ToString().Trim().ToLower())
									{
										isTableExist = true;
										break;
									}
									else
									{
										isTableExist = false;
									}
								}
								if(!isTableExist)
								{
										conn.Execute(stmt);
										SaveUpgradeLog("Upgrade Database: " + stmt);
								}
							}
							catch
							{
							}
							finally
							{
								if(conn1 != null)
								{
									conn1.Close();
									conn1 = null;
								}
							}
						}
						else if(Operate.Trim().ToLower() == "alter")                  //修改表结构
						{
							try
							{

								DataSet schemaTable = conn.GetDataSet("Select * from "+ table.Replace("dv_",Fetch.TablePrefix));
								if(columns.IndexOf("|") !=-1)
								{
									string[] arrSql = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									string[] arrCol = columns.Split('|');
									bool isExist = false;
									for(int i = 0;i<arrSql.Length;i++)
									{
										for(int k=0;k<schemaTable.Tables[0].Columns.Count;k++)
										{
											if(schemaTable.Tables[0].Columns[k].ColumnName.Trim().ToLower()==arrCol[i].Trim().ToLower())
											{
												isExist = true;
												break;
											}
											else
											{
												isExist = false;
											}
										}
										if(!isExist)                             //字段不存在
										{
											
											conn.Execute(arrSql[i].Replace("\r\n",""));
											SaveUpgradeLog("Upgrade Database: " + stmt);
										}
										if(arrSql[i].ToLower().IndexOf("alter",6) !=-1)
										{
											conn.Execute(arrSql[i].Replace("\r\n",""));
											SaveUpgradeLog("Upgrade Database: " + stmt);
										}
										
									}
								}
								else
								{
									bool isExist = false;                         //字段是否存在的标记
									for(int k=0;k<schemaTable.Tables[0].Columns.Count;k++)
									{
										if(schemaTable.Tables[0].Columns[k].ColumnName.Trim().ToLower()==columns.Trim().ToLower())
										{
											isExist = true;
											break;
										}
										else
										{
											isExist = false;
										}
									}

									if(!isExist)                             //字段不存在
									{
										conn.Execute(stmt);
										SaveUpgradeLog("Upgrade Database: " + stmt);
									}
									if(stmt.ToLower().IndexOf("alter",6)!=-1)
									{
										conn.Execute(stmt);
										SaveUpgradeLog("Upgrade Database: " + stmt);
									}
								}
							}
							catch
							{
							}
							finally
							{
								if(conn1 != null)
								{
									conn1.Close();
									conn1 = null;
								}
							}
						}
						else if(Operate.Trim().ToLower() == "update")
						{
							try
							{								
								if(sql.IndexOf("~") !=-1)
								{
									string tabusergroups = "dv_temp_usergroups";
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									for(int i = 0;i<arr.Length;i++)
									{
								
										conn.Execute(arr[i].Replace("\r\n",""));														
										SaveUpgradeLog("Upgrade Database: " + stmt);
											
									}									

								}
								else
								{
									conn.Execute(stmt);
									SaveUpgradeLog("Upgrade Database: " + stmt);
								}
							}
							catch
							{
							}
						}
						if(Operate.Trim().ToLower() == "insert")                 
						{
							try
							{								
								if(sql.IndexOf("~") !=-1)
								{
									string[] arr = System.Text.RegularExpressions.Regex.Split(stmt,"~",System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
									string tabHelp = "dv_help";
									if(table.Replace("dv_",Fetch.TablePrefix) == tabHelp.Replace("dv_",Fetch.TablePrefix))
									{	
										if((int)conn.GetScalar("select count(1) from "+ table.Replace("dv_",Fetch.TablePrefix) + " where H_ParentID=1 and H_Title='UBB语法'")==0)
										{
											for(int i = 0;i<arr.Length;i++)
											{

												conn.Execute(arr[i].Replace("\r\n",""));
												SaveUpgradeLog("Upgrade Database: " + stmt);
											}
										}
									}
									else
									{
										if((int)conn.GetScalar("select count(1) from "+table.Replace("dv_",Fetch.TablePrefix))==0)
										{
											for(int i = 0;i<arr.Length;i++)
											{
												
												conn.Execute(arr[i].Replace("\r\n",""));
												SaveUpgradeLog("Upgrade Database: " + stmt);
											}
										}
									}

								}
								else
								{
									string tabHelp = "dv_help";
									if(table.Replace("dv_",Fetch.TablePrefix)==tabHelp.Replace("dv_",Fetch.TablePrefix))
									{

										conn.Execute(stmt);
										SaveUpgradeLog("Upgrade Database: " + stmt);
									}
									else
									{
										if((int)conn.GetScalar("select count(1) from "+table.Replace("dv_",Fetch.TablePrefix))==0)
										{
											conn.Execute(stmt);
											SaveUpgradeLog("Upgrade Database: " + stmt);
										}
									}
								}
							}
							catch 
							{
							}
						}
					}
				}
			}
			catch 
			{
			}
		}
	}
	catch(Exception ex)
	{
		jsonReply.ErrorMsg = "升级数据库错误:" + ex.Message;
		jsonReply.RetVal = 0;
		SaveUpgradeLog("ERROR-->UpdateDataTableV1_0_0_2513_rc4():" + jsonReply.ErrorMsg);
		return false;
	}
	finally
	{
		if(reader !=null)
		reader = null;
		conn.Close();
	}
	SaveUpgradeLog("升级数据库成功!");
	return true;
}
</script>
	
关于我们 | 顾问团队 | 发展历程 | 联系我们 | 源码上传
联系电话(Tel):4008-010-151(免长途) 企业QQ:2852517674
地址:北京市海淀区中关村鼎好大厦A座二层 邮编:100080
Room A-801,Dinghao Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2015. 京ICP备09089570号 | 京公网安备11010702000869号
分享该页面
关闭侧边栏