void bgw_DoWork(object sender, DoWorkEventArgs e) { string[] rtlMsg = new string[2]; try { StringBuilder sbrlt = new StringBuilder(); TransferParams prm = e.Argument as TransferParams; BackgroundWorker bgw = sender as BackgroundWorker; StringBuilder tblScript = new StringBuilder(); string TargetDB = prm.TargetDB.DBAType.ToString(); var TblPkeys = prm.SourceDB.GetPrimarykey(); for (int i = 0; i < prm.TranTable.Count; i++) { ReportStatus RS = new ReportStatus() { Percent = i * 100 / prm.TranTable.Count, TransObject = prm.TranTable[i], TransType = "Table" }; bgw.ReportProgress(RS.Percent, RS); DataRow[] drs = prm.SrcTables.Select("TABLE_NAME ='" + prm.TranTable[i] + "'"); if (drs == null || drs.Length == 0) { continue; } DBColumnInfo[] ColumnInfo = new DBColumnInfo[drs.Length]; ODAParameter[] Oprms = new ODAParameter[drs.Length]; bool isBigData = false; try { for (int j = 0; j < drs.Length; j++) { int Scale = 0; int.TryParse(drs[j]["SCALE"].ToString().Trim(), out Scale); int length = 2000; int.TryParse(drs[j]["LENGTH"].ToString().Trim(), out length); string ColumnName = drs[j]["COLUMN_NAME"].ToString().Trim(); DBColumnInfo DBColInfo = new DBColumnInfo() { ColumnName = ColumnName, ColumnType = drs[j]["DATATYPE"].ToString().Trim(), Length = length, Scale = Scale, NotNull = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y", }; CurrentDatabase.GetTargetsType(prm.SourceDB.DBAType.ToString(), TargetDB, ref DBColInfo); ColumnInfo[j] = DBColInfo; isBigData = isBigData || DBColInfo.IsBigData; DBColumnInfo ODAColInfo = new DBColumnInfo() { ColumnName = ColumnName, ColumnType = drs[j]["DATATYPE"].ToString().Trim(), Length = length, Scale = Scale, NotNull = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y", }; CurrentDatabase.GetTargetsType(prm.SourceDB.DBAType.ToString(), "ODA", ref ODAColInfo); ODAdbType OdaType = ODAdbType.OVarchar; OdaType = (ODAdbType)Enum.Parse(typeof(ODAdbType), ODAColInfo.ColumnType, true); Oprms[j] = new ODAParameter() { ColumnName = drs[j]["COLUMN_NAME"].ToString(), DBDataType = OdaType, Direction = ParameterDirection.Input, ParamsName = drs[j]["COLUMN_NAME"].ToString(), Size = ColumnInfo[j].Length }; } } catch (Exception ex) { sbrlt.AppendLine(string.Format("分析表【{0}】字段时发生异常,异常信信:{1} ", prm.TranTable[i], ex.Message)); } string sql = ""; try { string[] Pkeys = null; if (TblPkeys != null && TblPkeys.ContainsKey(prm.TranTable[i])) { Pkeys = TblPkeys[prm.TranTable[i]]; } sql = this.CreateTable(prm.TargetDB, prm.TranTable[i], ColumnInfo, Pkeys); tblScript.AppendLine(sql); } catch (Exception ex) { sbrlt.AppendLine(string.Format("读取表【{0}】主键并生成建表脚本时生异常,异常信信:{1} ", prm.TranTable[i], ex.Message)); } try { if (prm.NeedTransTable) { if (prm.NeedTransTable) { try { string dropSQL = "DROP TABLE " + prm.TranTable[i]; prm.TargetDB.ExecuteSQL(dropSQL, null); } catch { } prm.TargetDB.ExecuteSQL(sql.ToString(), null); } ReportStatus RST = new ReportStatus() { Percent = (i + 1) * 100 / prm.TranTable.Count, TransObject = "Table [" + prm.TranTable[i] + "] Created", TransType = "Table" }; bgw.ReportProgress(RS.Percent, RST); } } catch (Exception ex) { sbrlt.AppendLine(string.Format("创建表【{0}】时发生异常,建表脚本 {1} ,异常信信:{2} ", prm.TranTable[i], sql.ToString(), ex.Message)); } try { if (prm.NeedTransData) { int total = 0; int maxR = isBigData ? 50 : 10000; int startIndx = 0; DataTable DT_total = CurrentDatabase.DataSource.Select("SELECT COUNT(*) FROM " + prm.TranTable[i], null); int.TryParse(DT_total.Rows[0][0].ToString(), out total); while (startIndx < total) { ReportStatus RSData0 = new ReportStatus() { Percent = total == 0 ? 0 : startIndx * 100 / total, TransObject = prm.TranTable[i] + " Preparing " + startIndx.ToString() + " ~ " + (startIndx + maxR).ToString() + "/" + total.ToString() + " record ", TransType = "Data" }; bgw.ReportProgress(RS.Percent, RSData0); DataTable Source = CurrentDatabase.DataSource.Select("SELECT * FROM " + prm.TranTable[i], null, startIndx, maxR, null); Source.TableName = prm.TranTable[i]; int endIdx = (startIndx + maxR) > total ? total : startIndx + maxR; ReportStatus RSData1 = new ReportStatus() { Percent = total == 0 ? 0 : endIdx * 100 / total, TransObject = prm.TranTable[i] + " Importing " + startIndx.ToString() + " ~ " + endIdx.ToString() + "/" + total.ToString() + " record ", TransType = "Data" }; bgw.ReportProgress(RS.Percent, RSData1); TarDB.Import(Source, Oprms); startIndx = startIndx + maxR; } } } catch (Exception ex) { sbrlt.AppendLine(string.Format("导入数据到表【{0}】时发生异常:{1} ", prm.TranTable[i], ex.Message)); } } if (sbrlt.Length == 0) { sbrlt.Append("数据复制完成!"); } rtlMsg[0] = sbrlt.ToString(); rtlMsg[1] = tblScript.ToString(); } catch (Exception ex) { rtlMsg[0] = ex.ToString(); } e.Result = rtlMsg; }
public string[] Generate_Code(params string[] TablesAndViews) { System.Text.StringBuilder strbldr = new System.Text.StringBuilder(); System.Text.StringBuilder ModelCode = new System.Text.StringBuilder(); strbldr.AppendLine("using System;"); strbldr.AppendLine("using System.Data;"); strbldr.AppendLine("using System.Collections.Generic;"); strbldr.AppendLine("using System.Reflection;"); strbldr.AppendLine("using NYear.ODA;"); strbldr.AppendLine("using NYear.ODA.Model;"); strbldr.AppendLine(); strbldr.AppendLine("namespace NYear.ODA.Cmd"); strbldr.AppendLine("{"); ModelCode.AppendLine("using System;"); ModelCode.AppendLine(); ModelCode.AppendLine("namespace NYear.ODA.Model"); ModelCode.AppendLine("{"); DataTable pdt_tables = _DBA.GetTableColumns(); DataTable pdt_views = _DBA.GetViewColumns(); for (int i = 0; i < TablesAndViews.Length; i++) { string TablePascalName = TablesAndViews[i]; System.Text.StringBuilder strCmd = new System.Text.StringBuilder(); System.Text.StringBuilder strModel = new System.Text.StringBuilder(); System.Text.StringBuilder GetColumnList = new StringBuilder(); GetColumnList.AppendLine("\t\t public override List<ODAColumns> GetColumnList() "); GetColumnList.AppendLine("\t\t { "); GetColumnList.Append("\t\t\t return new List<ODAColumns>() { "); DataRow[] drs = pdt_tables.Select("TABLE_NAME ='" + TablesAndViews[i] + "'"); if (drs.Length < 1) { drs = pdt_views.Select("TABLE_NAME ='" + TablesAndViews[i] + "'"); strCmd.AppendLine("\t\t public override bool Insert(params ODA.ODAColumns[] Cols) { throw new ODAException(\"Not suport Insert CmdName \" + CmdName);}"); strCmd.AppendLine("\t\t public override bool Update(params ODAColumns[] Cols) { throw new ODAException(\"Not Suport Update CmdName \" + CmdName);}"); strCmd.AppendLine("\t\t public override bool Delete() { throw new ODAException(\"Not Suport Delete CmdName \" + CmdName);}"); } for (int j = 0; j < drs.Length; j++) { string ColumnName = drs[j]["COLUMN_NAME"].ToString().Trim(); string ColumnPascalName = "Col" + this.Pascal(ColumnName); string ColumnCSharpDatatype = CurrentDatabase.GetTargetsType(drs[j]["DATATYPE"].ToString().Trim(), CurrentDatabase.DataSource.DBAType.ToString(), "CSHARP"); string ODAType = CurrentDatabase.GetTargetsType(drs[j]["DATATYPE"].ToString().Trim(), CurrentDatabase.DataSource.DBAType.ToString(), "ODA"); strModel.AppendLine("\t\t public " + ColumnCSharpDatatype + " " + ColumnName + " {get; set;}"); strCmd.AppendLine("\t\t public ODAColumns " + ColumnPascalName + "{ get { return new ODAColumns(this, \"" + ColumnName + "\", ODAdbType." + ODAType.Trim() + ", " + drs[j]["LENGTH"].ToString().Trim() + "," + (drs[j]["NOT_NULL"].ToString().Trim() == "Y"? "true":"false") + " ); } }"); GetColumnList.Append(ColumnPascalName + ","); } strCmd.AppendLine("\t\t public override string CmdName { get { return \"" + TablesAndViews[i].ToUpper() + "\"; }}"); strCmd.AppendLine(GetColumnList.Remove(GetColumnList.Length - 1, 1).ToString() + "};"); strCmd.AppendLine("\t\t }"); ModelCode.AppendLine("\tpublic partial class " + TablePascalName); ModelCode.AppendLine("\t{"); ModelCode.Append(strModel); ModelCode.AppendLine("\t}"); ////Cmd的代碼 strbldr.Append("\tinternal partial class "); strbldr.Append("Cmd" + Pascal(TablePascalName)); strbldr.AppendLine(":ORMCmd<" + TablePascalName + ">"); strbldr.AppendLine("\t{"); strbldr.Append(strCmd); strbldr.AppendLine("\t}"); } strbldr.AppendLine("}"); ModelCode.AppendLine("}"); return(new string[] { strbldr.ToString(), ModelCode.ToString() }); }
void bgw_DoWork(object sender, DoWorkEventArgs e) { string[] rtlMsg = new string[2]; try { StringBuilder sbrlt = new StringBuilder(); TransferParams prm = e.Argument as TransferParams; BackgroundWorker bgw = sender as BackgroundWorker; StringBuilder tblScript = new StringBuilder(); string TargetDB = prm.TargetDB.DBAType.ToString(); for (int i = 0; i < prm.TranTable.Count; i++) { ReportStatus RS = new ReportStatus() { Percent = i * 100 / prm.TranTable.Count, TransObject = prm.TranTable[i], TransType = "Table" }; bgw.ReportProgress(RS.Percent, RS); DataRow[] drs = prm.SrcTables.Select("TABLE_NAME ='" + prm.TranTable[i] + "'"); if (drs == null || drs.Length == 0) { continue; } DatabaseColumnInfo[] ColumnInfo = new DatabaseColumnInfo[drs.Length]; ODAParameter[] Oprms = new ODAParameter[drs.Length]; bool isBigData = false; for (int j = 0; j < drs.Length; j++) { int Scale = 0; int.TryParse(drs[j]["SCALE"].ToString().Trim(), out Scale); int length = 2000; int.TryParse(drs[j]["LENGTH"].ToString().Trim(), out length); string TargetDBDataType = CurrentDatabase.GetTargetsType(drs[j]["DATATYPE"].ToString().Trim(), prm.TargetDB.DBAType.ToString(), TargetDB); string ODAType = CurrentDatabase.GetTargetsType(drs[j]["DATATYPE"].ToString().Trim(), CurrentDatabase.DataSource.DBAType.ToString(), "ODA"); ODAdbType DBDataType = ODAdbType.OVarchar; Enum.TryParse <ODAdbType>(ODAType, true, out DBDataType); ColumnInfo[j] = new DatabaseColumnInfo() { ColumnType = TargetDBDataType, Length = length <= 0 ? 2000 : length, Name = prm.TargetDB.ToDBColumnName(drs[j]["COLUMN_NAME"].ToString()), NotNull = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y", Scale = Scale, NoLength = DBDataType == ODAdbType.OBinary || DBDataType == ODAdbType.OInt || DBDataType == ODAdbType.ODatetime }; if (DBDataType == ODAdbType.OBinary) { isBigData = true; } Oprms[j] = new ODAParameter() { ColumnName = drs[j]["COLUMN_NAME"].ToString(), DBDataType = DBDataType, Direction = ParameterDirection.Input, ParamsName = drs[j]["COLUMN_NAME"].ToString(), Size = ColumnInfo[j].Length }; } string[] Pkeys = prm.SourceDB.GetPrimarykey(prm.TranTable[i]); for (int k = 0; Pkeys != null && Pkeys.Length > k; k++) { Pkeys[k] = prm.TargetDB.ToDBColumnName(Pkeys[k]); } string sql = this.CreateTable(prm.TargetDB, prm.TranTable[i], ColumnInfo, Pkeys); tblScript.AppendLine(sql); if (prm.NeedTransTable) { if (prm.NeedTransTable) { try { string dropSQL = "DROP TABLE " + prm.TranTable[i]; prm.TargetDB.ExecuteSQL(dropSQL, null); } catch { } prm.TargetDB.ExecuteSQL(sql.ToString(), null); } ReportStatus RST = new ReportStatus() { Percent = (i + 1) * 100 / prm.TranTable.Count, TransObject = "Table [" + prm.TranTable[i] + "] Created", TransType = "Table" }; bgw.ReportProgress(RS.Percent, RST); } if (prm.NeedTransData) { int total = 0; int maxR = isBigData ? 50 : 10000; int startIndx = 0; DataTable DT_total = CurrentDatabase.DataSource.Select("SELECT COUNT(*) FROM " + prm.TranTable[i], null); int.TryParse(DT_total.Rows[0][0].ToString(), out total); while (startIndx < total) { ReportStatus RSData0 = new ReportStatus() { Percent = total == 0 ? 0 : startIndx * 100 / total, TransObject = prm.TranTable[i] + " Preparing " + startIndx.ToString() + " ~ " + (startIndx + maxR).ToString() + "/" + total.ToString() + " record ", TransType = "Data" }; bgw.ReportProgress(RS.Percent, RSData0); DataTable Source = CurrentDatabase.DataSource.Select("SELECT * FROM " + prm.TranTable[i], null, startIndx, maxR, null); Source.TableName = prm.TranTable[i]; int endIdx = (startIndx + maxR) > total ? total : startIndx + maxR; ReportStatus RSData1 = new ReportStatus() { Percent = total == 0 ? 0 : endIdx * 100 / total, TransObject = prm.TranTable[i] + " Importing " + startIndx.ToString() + " ~ " + endIdx.ToString() + "/" + total.ToString() + " record ", TransType = "Data" }; bgw.ReportProgress(RS.Percent, RSData1); TarDB.Import(Source, Oprms); startIndx = startIndx + maxR; } } } if (sbrlt.Length == 0) { sbrlt.Append("数据复制完成!"); } rtlMsg[0] = sbrlt.ToString(); rtlMsg[1] = tblScript.ToString(); } catch (Exception ex) { rtlMsg[0] = ex.ToString(); } e.Result = rtlMsg; }