Ver código fonte

【修改】清空数据库:初始化操作中增加清除库的触发器和约束方法;

新疆警官学校中职
dyy 1 ano atrás
pai
commit
ef2075c858
1 arquivos alterados com 114 adições e 2 exclusões
  1. +114
    -2
      Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Application.Module/Learun.Application.Base/SystemModule/DatabaseInit/DatabaseInitService.cs

+ 114
- 2
Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Application.Module/Learun.Application.Base/SystemModule/DatabaseInit/DatabaseInitService.cs Ver arquivo

@@ -3,6 +3,7 @@ using Learun.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Learun.Application.Base.SystemModule
@@ -50,14 +51,83 @@ namespace Learun.Application.Base.SystemModule
string backupSql2 = "backup database " + databaseName2 + " to disk='" + filePath2 + "' with format;";
this.BaseRepository("CollegeMIS").ExecuteAsyncBySql(backupSql2);

//清除业务数据
//清除触发器
if (true)
{
var strSql1 = new StringBuilder();
strSql1.Append(@"select a.name,a.id,a.xtype,a.parent_obj,b.name as pname,b.id as pid,b.xtype as pxtype,b.type as ptype,
'drop trigger ' + a.name + ';' as sql
from sysobjects a
left join sysobjects b on a.parent_obj = b.id
where a.xtype = 'TR'
and b.name not like '%sys%' and b.name not like '%dt%'
order by b.name
; ");
//adms7ultimate2表
var sqlList = this.BaseRepository().FindList<SysobjectModel>(strSql1.ToString()).Select(x=>x.sql);
//清除
if (sqlList.Any())
{
foreach (var item in sqlList)
{
this.BaseRepository().ExecuteBySql(item);
}
}
//CollegeMIS表
var sqlList2 = this.BaseRepository("CollegeMIS").FindList<SysobjectModel>(strSql1.ToString()).Select(x => x.sql);
//清除
if (sqlList2.Any())
{
foreach (var item in sqlList2)
{
this.BaseRepository("CollegeMIS").ExecuteBySql(item);
}
}
}

//清除约束
if (true)
{
var strSql1 = new StringBuilder();
strSql1.Append(@"select a.name,a.id,a.xtype,a.parent_obj,b.name as pname,b.id as pid,b.xtype as pxtype,b.type as ptype,
'alter table '+b.name+' drop constraint '+a.name+';' as sql
from sysobjects a
left join sysobjects b on a.parent_obj=b.id
where a.xtype in ('F','C','D','UQ')
and b.name not like '%sys%' and b.name not like '%dt%'
order by b.name
;");
//adms7ultimate2表
var sqlList = this.BaseRepository().FindList<SysobjectModel>(strSql1.ToString()).Select(x => x.sql);
//清除
if (sqlList.Any())
{
foreach (var item in sqlList)
{
this.BaseRepository().ExecuteBySql(item);
}
}
//CollegeMIS表
var sqlList2 = this.BaseRepository("CollegeMIS").FindList<SysobjectModel>(strSql1.ToString()).Select(x => x.sql);
//清除
if (sqlList2.Any())
{
foreach (var item in sqlList2)
{
this.BaseRepository("CollegeMIS").ExecuteBySql(item);
}
}
}


//清除业务数据(当表有触发器和约束时,会执行异常!错误信息为:“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”)
if (true)
{
var strSql1 = new StringBuilder();
strSql1.Append("declare @trun_name varchar(50) ");
strSql1.Append("declare name_cursor cursor for ");
strSql1.Append("select 'truncate table ' + name+';' from sysobjects where xtype='U' and name <> 'LR_Base_User' and name <> 'LR_Base_Role' and name<>'LR_App_Function' and name<>'LR_Base_Authorize' and name<>'LR_Base_DatabaseLink' ");
strSql1.Append(" and name<>'LR_Base_DataCondition' and name<>'LR_Base_DataItem' and name<>'LR_Base_DataItemDetail' and name<>'LR_Base_DataRelation' and name<>'LR_Base_DataSource' ");
strSql1.Append(" and name<>'LR_Base_DataCondition' and name<>'LR_Base_DataItem' and name<>'LR_Base_DataItemDetail' and name<>'LR_Base_DataRelation' and name<>'LR_Base_DataSource' and name<>'LR_Base_Area' ");
strSql1.Append(" and name<>'LR_Base_DbField' and name<>'LR_Base_Interface' and name<>'LR_Base_Logo' and name<>'LR_Base_Module' and name<>'LR_Base_ModuleButton' and name<>'LR_Base_ModuleColumn' ");
strSql1.Append(" and name<>'LR_Base_ModuleForm' and name<>'LR_Base_UserRelation' and name<>'LR_DT_Chart' and name<>'LR_DT_List' and name<>'LR_DT_Target' and name<>'LR_Excel_Export' and name<>'LR_Excel_Import' ");
strSql1.Append(" and name<>'LR_Excel_ImportFileds' and name<>'LR_Form_Relation' and name<>'LR_Form_Scheme' and name<>'LR_Form_SchemeInfo' and name<>'LR_Lg_Map' and name<>'LR_Lg_Type' and name<>'LR_NWF_Scheme' ");
@@ -103,6 +173,48 @@ namespace Learun.Application.Base.SystemModule
}
}

/// <summary>
/// 视图临时模型
/// </summary>
public class SysobjectModel {
/// <summary>
/// 触发器、约束名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 触发器、约束id
/// </summary>
public string id { get; set; }
/// <summary>
/// 类型
/// </summary>
public string xtype { get; set; }
/// <summary>
/// 触发器、约束所属表id
/// </summary>
public string parent_obj { get; set; }
/// <summary>
/// 触发器、约束所属表名
/// </summary>
public string pname { get; set; }
/// <summary>
/// 表id
/// </summary>
public string pid { get; set; }
/// <summary>
/// 表类型
/// </summary>
public string pxtype { get; set; }
/// <summary>
/// 表类型
/// </summary>
public string ptype { get; set; }
/// <summary>
/// 删除语句
/// </summary>
public string sql { get; set; }
}






Carregando…
Cancelar
Salvar