private void tsmiConnect_Click(object sender, EventArgs e) { FrmOpenDb openDb = new FrmOpenDb(); if (_dbClass != null) { openDb.SetDbType(_dbClass.GetClassDbType(), true); openDb.DbConnectConfigure = _dbConnectConfigure; } DialogResult dr = openDb.ShowDialog(this); if (dr == DialogResult.Cancel) { return; } else { CloseDb(); _dbConnectConfigure = openDb.DbConnectConfigure; SetTitle(); if (dr == DialogResult.OK) { SetDbClass(openDb.DbClass); ReInit(); } else { this.DbConnectConfigure = _dbConnectConfigure; } if (NewConnect != null) { NewConnect(this, e); } } }
private void LoadSql() { StringBuilder sb = new StringBuilder(); sb.AppendLine(CreateSqlObject.ToCollectionSqls(MyDbHelper.GetCreateSqlFunction(_table, _dbClass.GetClassDbType())(null))); List <IConstraintClass> ccs = _dbClass.GetConstraints(_table.TableName); foreach (IConstraintClass item in ccs) { sb.AppendLine(CreateSqlObject.ToCollectionSqls( MyDbHelper.GetCreateSqlFunction(item, _dbClass.GetClassDbType())(null) )); } List <IIndexClass> ics = _dbClass.GetIndexs(_table.TableName); foreach (IIndexClass item in ics) { sb.AppendLine(CreateSqlObject.ToCollectionSqls( MyDbHelper.GetCreateSqlFunction(item, _dbClass.GetClassDbType())(null) )); } tbSql.Text = sb.ToString().TrimEnd('\r', '\n'); }
private void FrmNormalView_Load(object sender, EventArgs e) { IGetAttribute attri = _object as IGetAttribute; if (attri != null) { this.dgvNormal.DataSource = NameAliasValue.ToDataTable(attri.GetAttributes()); } ICreateSql creatsql = _object as ICreateSql; if (creatsql != null) { CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(creatsql, _dbClass.GetClassDbType()); if (action != null) { List <CreateSqlObject> csos = action(_dbClass.GetCurrentTableSpaceName()); tbSql.Text = CreateSqlObject.ToCollectionSqls(csos); } } }
public void ReadToDb(IDbClass dbClass, ReadToDbExceptionHandle exceptionHandle = null, bool isReadTables = true, bool isReadConstraints = true, bool isReadSequences = true, bool isReadTriggers = true, bool isReadIndexes = true, bool isReadFunctions = true, bool isReadProcedures = true, bool isReadJavaSources = true, bool isReadDatas = true) { int process_start = 0; int process_end = 3; #region 配置文件打开预加载 SetPro(process_start, "开始导入数据库文件..."); if (_dbtFile == null) { SetPro(100, "数据库配置不正确!"); return; } process_start = 1; SetPro(process_start, "数据库配置:" + _dbtFile.ToString()); if (_dbDefine == null) { SetPro(process_start, "开始读取数据库文件定义..."); ReadDbDefinition(); } process_start = 2; SetPro(process_start); if (_dbDefine == null) { SetPro(100, "数据库文件定义不正确!"); return; } #endregion SetPro(process_end, "读取到数据库定义:" + _dbDefine.ToString()); process_start = process_end; process_end = 4; #region 获取当前表空间 SetPro(process_start, "开始获取当前表空间..."); string tableSpaceName = null; try { tableSpaceName = dbClass.GetCurrentTableSpaceName(); } catch (Exception ex) { if (!DoProToDbException("读取表空间异常", exceptionHandle, ex)) { return; } } SetPro(process_end, "获取到当前表空间:" + tableSpaceName); #endregion SetPro(process_end); process_start = process_end; process_end = 20; #region 导入表 if (isReadTables) { SetPro(process_start, "待导入表的数目:" + _dbDefine.Tables.Count); if (_dbDefine.Tables.Count > 0) { SetPro(process_start, "开始导入表定义..."); float dd = (process_end - process_start) / (float)_dbDefine.Tables.Count; int dtcount = 0; foreach (ITableClass item in _dbDefine.Tables) { dtcount++; int pro = process_start + (int)(dtcount * dd); SetPro(pro, "开始导入表:" + item.TableName); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入表错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } } } #endregion SetPro(process_end); process_start = process_end; process_end = 60; #region 导入数据 if (isReadDatas) { DbData data = this.GetNextDataRow(); int tablesCount = _dbDefine.Tables.Count == 0 ? 1 : _dbDefine.Tables.Count; float dd = (process_end - process_start) / (float)tablesCount; int dtcount = 0; if (data != null) { SetPro(20, "开始导入数据..."); int count = -1; string tablename = null; while (data != null) { if (count == -1) { int add = (int)(dtcount * dd); if (add > process_end) { add = process_end; } dtcount++; tablename = data.TableDataRow.tableName; SetPro(process_start + add, "开始导入表:" + tablename + " 的数据...待导入数目:" + curTableDataCount); count = 0; } else if (data.TableDataRow.tableName != tablename) { int add = (int)(dtcount * dd); if (add > process_end) { add = process_end; } SetPro(process_start + add, "导入表:" + tablename + " 的数据个数为:" + count); tablename = data.TableDataRow.tableName; SetPro(process_start + add, "开始导入表:" + tablename + " 的数据...待导入数目:" + curTableDataCount); count = 0; dtcount++; } CreateDataSqlDelegate action = MyDbHelper.GetCreateSqlFunction(data, dbClass.GetClassDbType()); CreateDataSqlParams sqlpram = action(); try { dbClass.GetDbHelper().ExecuteSql(sqlpram.sql, sqlpram.sql_params); } catch (Exception ex) { if (!DoProToDbException("导入数据错误", exceptionHandle, ex, "执行的SQL:" + sqlpram.sql + "\r\n数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(data.TableDataRow.base64TableValues))) { return; } } count++; data = this.GetNextDataRow(); } SetPro(process_end, "导入表:" + tablename + " 的数据个数为:" + count); SetPro(process_end, "导入数据结束."); } } #endregion SetPro(process_end); process_start = process_end; process_end = 70; #region 导入索引 if (isReadIndexes) { SetPro(process_start, "待导入索引个数:" + _dbDefine.Indexes.Count); if (_dbDefine.Indexes.Count > 0) { SetPro(process_start, "开始导入索引..."); float dd = (process_end - process_start) / (float)_dbDefine.Indexes.Count; int dtcount = 0; foreach (IIndexClass item in _dbDefine.Indexes) { dtcount++; SetPro(process_start + (int)(dtcount * dd), "开始导入表:" + item.Table_Name + " 的索引:" + item.Name); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入索引错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } SetPro(process_end, "导入索引结束."); } } #endregion SetPro(process_end); process_start = process_end; process_end = 80; #region 导入约束 if (isReadConstraints) { SetPro(process_start, "待导入表的约束个数:" + _dbDefine.Constraints.Count); if (_dbDefine.Constraints.Count > 0) { SetPro(process_start, "开始导入表约束..."); float dd = (process_end - process_start) / (float)_dbDefine.Constraints.Count; int dtcount = 0; IOrderedEnumerable <IConstraintClass> ordered = _dbDefine.Constraints.OrderBy(m => m.Level); foreach (IConstraintClass item in ordered) { dtcount++; int pro = process_start + (int)(dtcount * dd); SetPro(pro, "开始导入表:" + item.Table_Name + " 的约束:" + item.Name); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入约束错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } SetPro(process_end, "导入约束结束."); } } #endregion SetPro(process_end); process_start = process_end; process_end = 85; #region 导入序列 if (isReadSequences) { SetPro(process_start, "待导入序列个数:" + _dbDefine.Sequences.Count); if (_dbDefine.Sequences.Count > 0) { SetPro(process_start, "开始导入序列..."); float dd = (process_end - process_start) / (float)_dbDefine.Sequences.Count; int dtcount = 0; foreach (ISequenceClass item in _dbDefine.Sequences) { dtcount++; SetPro(process_start + (int)(dtcount * dd), "开始导入序列:" + item.SequenceName); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (Exception ex) { if (!DoProToDbException("导入序列错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } SetPro(process_end, "导入序列结束."); } } #endregion SetPro(process_end); process_start = process_end; process_end = 90; #region 导入触发器 if (isReadTriggers) { SetPro(process_start, "待导入触发器个数:" + _dbDefine.Triggers.Count); if (_dbDefine.Triggers.Count > 0) { float dd = (process_end - process_start) / (float)_dbDefine.Triggers.Count; int dtcount = 0; SetPro(80, "开始导入触发器..."); foreach (ITriggerClass item in _dbDefine.Triggers) { dtcount++; SetPro(process_start + (int)(dtcount * dd), "开始导入表:" + item.Table_Name + " 的触发器:" + item.Name); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入触发器错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } } SetPro(process_end, "导入触发器结束."); } #endregion SetPro(process_end); process_start = process_end; process_end = 95; #region 导入过程 if (isReadProcedures) { SetPro(process_start, "待导入存储过程个数:" + _dbDefine.Procedures.Count); if (_dbDefine.Procedures.Count > 0) { float dd = (process_end - process_start) / (float)_dbDefine.Procedures.Count; int dtcount = 0; SetPro(process_start, "开始导入存储过程..."); foreach (IProcedureClass item in _dbDefine.Procedures) { dtcount++; SetPro(process_start + (int)(dtcount * dd), "开始导入存储过程:" + item.Name); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入存储过程错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } SetPro(process_end, "导入过程结束."); } } #endregion SetPro(process_end); process_start = process_end; process_end = 97; #region 导入函数 if (isReadFunctions) { SetPro(process_start, "待导入函数个数:" + _dbDefine.Functions.Count); if (_dbDefine.Functions.Count > 0) { float dd = (process_end - process_start) / (float)_dbDefine.Functions.Count; int dtcount = 0; SetPro(process_start, "开始导入函数..."); foreach (IFunctionClass item in _dbDefine.Functions) { dtcount++; SetPro(process_start + (int)(dtcount * dd), "开始导入函数:" + item.Name); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入函数错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } SetPro(process_end, "导入函数结束."); } } #endregion SetPro(process_end); process_start = process_end; process_end = 100; #region 导入java资源 if (isReadJavaSources) { SetPro(process_start, "待导入Java资源个数:" + _dbDefine.JavaSources.Count); if (_dbDefine.JavaSources.Count > 0) { float dd = (process_end - process_start) / (float)_dbDefine.JavaSources.Count; int dtcount = 0; SetPro(95, "开始导入Java资源..."); foreach (IJavaSourceClass item in _dbDefine.JavaSources) { dtcount++; SetPro(95 + (int)(dtcount * dd), "开始导入java资源:" + item.Name); CreateSqlDelegate action = MyDbHelper.GetCreateSqlFunction(item, dbClass.GetClassDbType()); List <CreateSqlObject> sqlObjs = action(tableSpaceName); foreach (CreateSqlObject sql in sqlObjs) { try { dbClass.GetDbHelper().ExecuteSql(sql.sql); } catch (System.Exception ex) { if (!DoProToDbException("导入Java资源错误", exceptionHandle, ex, "执行的SQL:" + sql.sql)) { return; } } } } SetPro(process_end, "导入Java资源结束."); } } #endregion SetPro(process_end, "导入结束."); }
public void WriteDb(IDbClass dbClass, DbClassSelected selected) { SetPro(0, "开始导出数据库类型..."); BeginWrite(DbtFileTags.DbTFile); //写入类型 BeginWrite(DbtFileTags.DbType); Write(dbClass.GetClassDbType()); EndWrite(DbtFileTags.DbType); BeginWrite(DbtFileTags.Version); Write(dbClass.GetDbVersionString()); EndWrite(DbtFileTags.Version); SetPro(2, "导出数据库类型结束!"); if (selected.isTablesChecked) { SetPro(2, "开始获取表..."); //写入表 List <ITableClass> tables = dbClass.GetTables(); SetPro(10, "获取表个数:" + tables.Count); if (tables.Count > 0) { SetPro(10, "开始导出表..."); BeginWrite(DbtFileTags.Tables); foreach (ITableClass item in tables) { SetPro(15, "开始导出表:" + item.TableName); BeginWrite(DbtFileTags.Table); Write(item); EndWrite(DbtFileTags.Table); } EndWrite(DbtFileTags.Tables); SetPro(25, "导出表结束!"); } } SetPro(25, ""); if (selected.isConstraintsChecked) { SetPro(25, "开始获取约束..."); //写入约束 List <IConstraintClass> conss = dbClass.GetConstraints(); SetPro(28, "获取约束个数:" + conss.Count); if (conss.Count > 0) { SetPro(28, "开始导出约束..."); BeginWrite(DbtFileTags.Constraints); foreach (IConstraintClass item in conss) { SetPro(30, "导出约束:" + item.Name); BeginWrite(DbtFileTags.Constraint); Write(item); EndWrite(DbtFileTags.Constraint); } EndWrite(DbtFileTags.Constraints); SetPro(35, "导出约束结束!"); } } SetPro(35, ""); if (selected.isSequencesChecked) { SetPro(35, "开始获取序列..."); //写入序列 List <ISequenceClass> seqs = dbClass.GetSequences(); SetPro(37, "获取序列个数:" + seqs.Count); if (seqs.Count > 0) { SetPro(37, "开始导出序列..."); BeginWrite(DbtFileTags.Sequences); foreach (ISequenceClass item in seqs) { SetPro(39, "导出序列:" + item.SequenceName); BeginWrite(DbtFileTags.Sequence); Write(item); EndWrite(DbtFileTags.Sequence); } EndWrite(DbtFileTags.Sequences); SetPro(40, "结束导出序列."); } } SetPro(40, ""); if (selected.isTriggersChecked) { SetPro(40, "开始获取触发器..."); //写入触发器 List <ITriggerClass> tris = dbClass.GetTriggers(); SetPro(41, "获取触发器个数:" + tris.Count); if (tris.Count > 0) { SetPro(41, "开始导出触发器..."); BeginWrite(DbtFileTags.Triggers); foreach (ITriggerClass item in tris) { SetPro(43, "开始导出触发器..."); BeginWrite(DbtFileTags.Trigger); Write(item); EndWrite(DbtFileTags.Trigger); } EndWrite(DbtFileTags.Triggers); SetPro(44, "结束导出触发器."); } } SetPro(44, ""); if (selected.isIndexesChecked) { SetPro(44, "开始获取索引..."); //写入索引 List <IIndexClass> inds = dbClass.GetIndexs(); SetPro(45, "获取索引个数:" + inds.Count); if (inds.Count > 0) { SetPro(45, "开始导出索引..."); BeginWrite(DbtFileTags.Indexes); foreach (IIndexClass item in inds) { SetPro(46, "导出索引:" + item.Name); BeginWrite(DbtFileTags.Index); Write(item); EndWrite(DbtFileTags.Index); } EndWrite(DbtFileTags.Indexes); SetPro(47, "导出索引结束."); } } SetPro(47, ""); if (selected.isFunctionsChecked) { SetPro(47, "开始获取函数..."); //写入函数 List <IFunctionClass> funs = dbClass.GetFunctions(); SetPro(48, "获取函数个数:" + funs.Count); if (funs.Count > 0) { SetPro(48, "开始导出函数..."); BeginWrite(DbtFileTags.Functions); foreach (IFunctionClass item in funs) { SetPro(49, "导出函数:" + item.Name); BeginWrite(DbtFileTags.Function); Write(item); EndWrite(DbtFileTags.Function); } EndWrite(DbtFileTags.Functions); SetPro(50, "导出函数结束."); } } SetPro(50, ""); if (selected.isProceduresChecked) { SetPro(50, "开始获取过程..."); //写入过程 List <IProcedureClass> pros = dbClass.GetProcedures(); SetPro(51, "获取过程个数:" + pros.Count); if (pros.Count > 0) { SetPro(51, "开始导出过程..."); BeginWrite(DbtFileTags.Procedures); foreach (IProcedureClass item in pros) { SetPro(51, "导出过程:" + item.Name); BeginWrite(DbtFileTags.Procedure); Write(item); EndWrite(DbtFileTags.Procedure); } EndWrite(DbtFileTags.Procedures); SetPro(52, "导出过程结束."); } } SetPro(52, ""); if (selected.isJavaSourcesChecked) { SetPro(52, "开始获取java资源..."); //写入Java资源 List <IJavaSourceClass> javas = dbClass.GetJavaSources(); SetPro(53, "获取java资源个数:" + javas.Count); if (javas.Count > 0) { SetPro(53, "开始导出java资源..."); BeginWrite(DbtFileTags.JavaSources); foreach (IJavaSourceClass item in javas) { SetPro(53, "导出java资源:" + item.Name); BeginWrite(DbtFileTags.JavaSource); Write(item); EndWrite(DbtFileTags.JavaSource); } EndWrite(DbtFileTags.JavaSources); SetPro(54, "导出java资源结束."); } } SetPro(54, ""); int perleft = 100 - 54; if (selected.isDatasChecked) { SetPro(54, "开始导出数据..."); //写入数据 List <ITableClass> tables = dbClass.GetTables(); if (tables.Count > 0) { float dper = perleft / (float)tables.Count; int tcount = 0; BeginWrite(DbtFileTags.Datas); foreach (ITableClass item in tables) { tcount++; decimal totalDataCount = dbClass.GetTableDataCount(item.TableName); SetPro(54 + (int)(tcount * dper), "开始导出表:" + item.TableName + " 的数据...,需导出总数:" + totalDataCount); if (totalDataCount == 0) { SetPro(-1, "表 " + item.TableName + " 无数据导出!"); continue; } List <DbData> list = dbClass.GetTableData(item.TableName, 0, 50); if (list.Count > 0) { BeginWrite(DbtFileTags.Data); BeginWrite(DbtFileTags.DataTableName); Write(list[0].TableDataRow.tableName); EndWrite(DbtFileTags.DataTableName); BeginWrite(DbtFileTags.DataColumnType); Write(list[0].TableDataRow.tableColumnTypes); EndWrite(DbtFileTags.DataColumnType); BeginWrite(DbtFileTags.DataColumnDbType); Write(list[0].TableDataRow.tableColumnDbTypes); EndWrite(DbtFileTags.DataColumnDbType); BeginWrite(DbtFileTags.DataCulumnName); Write(list[0].TableDataRow.tableColumnNames); EndWrite(DbtFileTags.DataCulumnName); BeginWrite(DbtFileTags.DataCount); Write(totalDataCount); EndWrite(DbtFileTags.DataCount); int start = 0; do { foreach (DbData data in list) { BeginWrite(DbtFileTags.DataValue); Write(data.TableDataRow.base64TableValues); EndWrite(DbtFileTags.DataValue); } if (list.Count != 50) { SetPro(54 + (int)(tcount * dper), "导出表:" + item.TableName + " 的数据个数为:" + (start + list.Count)); break; } else { start += 50; list = dbClass.GetTableData(item.TableName, start, 50); } } while (true); EndWrite(DbtFileTags.Data); } } EndWrite(DbtFileTags.Datas); } } EndWrite(DbtFileTags.DbTFile); SetPro(100, "导出结束."); }