Beispiel #1
0
        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                StringBuilder    sbrlt = new StringBuilder();
                TransferParams   prm   = e.Argument as TransferParams;
                BackgroundWorker bgw   = sender as BackgroundWorker;

                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);
                        ColumnInfo[j] = prm.TargetDB.ODAColumnToOrigin(drs[j]["COLUMN_NAME"].ToString(), drs[j]["ODA_DATATYPE"].ToString().Trim(), length, Scale);

                        ColumnInfo[j].NotNull = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y";
                        ODAdbType DBDataType = ODAdbType.OVarchar;
                        Enum.TryParse <ODAdbType>(drs[j]["ODA_DATATYPE"].ToString().Trim(), out DBDataType);

                        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
                        };
                    }
                    if (prm.NeedTransTable)
                    {
                        string[] Pkeys = prm.SourceDB.GetPrimarykey(prm.TranTable[i]);
                        string   tlt   = this.CreateTable(prm.TargetDB, prm.TranTable[i], ColumnInfo, Pkeys);
                        if (!string.IsNullOrWhiteSpace(tlt))
                        {
                            sbrlt.AppendLine(tlt);
                            continue;
                        }
                        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)
                    {
                        for (int j = 0; j < drs.Length; j++)
                        {
                            Oprms[j] = new ODAParameter()
                            {
                                ColumnName = drs[j]["COLUMN_NAME"].ToString(),
                                DBDataType = (ODAdbType)Enum.Parse(typeof(ODAdbType), drs[j]["ODA_DATATYPE"].ToString().Trim()),
                                Direction  = ParameterDirection.Input,
                                ParamsName = drs[j]["COLUMN_NAME"].ToString(),
                                Size       = ColumnInfo[j].Length
                            };
                        }

                        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("数据复制完成!");
                }
                e.Result = sbrlt.ToString();
            }
            catch (Exception ex)
            {
                e.Result = ex.ToString();
            }
        }
Beispiel #2
0
        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;
        }