/// <summary>
        ///
        /// </summary>
        /// <param name="srcHandler"></param>
        /// <param name="targetHandler"></param>
        /// <param name="tableName">TableName is the file name</param>
        private void PreProcessCSV(CoreEA.ICoreEAHander srcHandler, CoreEA.ICoreEAHander targetHandler, string tableName)
        {
            try
            {
                BaseTableSchema tableSchmea = srcHandler.GetTableSchemaInfoObject(tableName);

                if (tableName.Contains(" "))
                {
                    tableName = tableName.Replace(" ", "");
                }
                using (StreamWriter sw = File.CreateText(targetHandler.CurDatabase.Append(tableName + ".csv")))
                {
                    //Create Column Header
                    StringBuilder sb = new StringBuilder();
                    tableSchmea.Columns.ForEach((column) =>
                    {
                        sb.Append(column.ColumnName);
                        sb.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                    });
                    sw.WriteLine(sb.ToString());

                    //Create Data
                    DataTable dt = srcHandler.GetAllDataFromTable(tableName);

                    foreach (DataRow row in dt.Rows)
                    {
                        StringBuilder builder = new StringBuilder();
                        foreach (var column in tableSchmea.Columns)
                        {
                            builder.Append(row[column.ColumnName]);
                            builder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                        }
                        sw.WriteLine(builder.ToString());
                    }
                }
            }
            catch (Exception accessEx)
            {
                accessEx.HandleMyException();
                return;
            }
            finally
            {
                butNext.Content   = "End";
                butNext.IsEnabled = false;
            }
        }
示例#2
0
 public TargetServer(CoreEA.ICoreEAHander inSrcCore, List <string> inTableList)
 {
     srcCore   = inSrcCore;
     tableList = inTableList;
     InitializeComponent();
 }
        /// <summary>
        /// Process CSV Type
        /// All args are checked external
        /// So here is no need to do again
        /// </summary>
        /// <param name="srcHandler"></param>
        /// <param name="targetHandler"></param>
        /// <param name="tableList"></param>
        private void PreProcessCSV(CoreEA.ICoreEAHander srcHandler, CoreEA.ICoreEAHander targetHandler, List <string> tableList)
        {
            List <SyncResultArgs> result = new List <SyncResultArgs>();

            tableList.ForEach((tableName) =>
            {
                SyncResultArgs curStatus = new SyncResultArgs();
                try
                {
                    BaseTableSchema tableSchmea = srcHandler.GetTableSchemaInfoObject(tableName);

                    if (tableName.Contains(" "))
                    {
                        tableName = tableName.Replace(" ", "");
                    }
                    using (StreamWriter sw = File.CreateText(targetHandler.CurDatabase.Append(tableName + ".csv")))
                    {
                        //Create Column Header
                        StringBuilder sb = new StringBuilder();
                        tableSchmea.Columns.ForEach((column) =>
                        {
                            sb.Append(column.ColumnName);
                            sb.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                        });
                        sw.WriteLine(sb.ToString());

                        //Create Data
                        DataTable dt = srcHandler.GetAllDataFromTable(tableName);

                        foreach (DataRow row in dt.Rows)
                        {
                            StringBuilder builder = new StringBuilder();
                            foreach (var column in tableSchmea.Columns)
                            {
                                builder.Append(row[column.ColumnName]);
                                builder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                            }
                            sw.WriteLine(builder.ToString());
                        }
                    }

                    curStatus.ProcessStatus = true;
                    curStatus.TableName     = tableName;
                }
                catch (Exception accessEx)
                {
                    curStatus.LastErrorMsg  = accessEx.Message;
                    curStatus.ProcessStatus = false;
                    curStatus.TableName     = tableName;

                    accessEx.HandleMyException();
                    return;
                }
                finally
                {
                    result.Add(curStatus);
                    butNext.Content   = "End";
                    butNext.IsEnabled = false;
                }
            });

            if ("DataTransfer_ConfirmViewReport".GetFromResourece().Confirm())
            {
                ShowSyncResult rShow = new ShowSyncResult(result);
                rShow.ShowDialog();
            }
            else
            {
            }

            Close();
        }
        /// <summary>
        /// Finish Step
        /// Notice : Each table schema from Core can't be changed .
        /// We will filter the table schema for each target handler in this method.
        /// </summary>
        /// <param name="srcHandler"></param>
        /// <param name="targetHandler"></param>
        /// <param name="tableList"></param>
        /// <param name="isShowUI">This value will be false when UT</param>
        private bool DoExchangeData(CoreEA.ICoreEAHander srcHandler, CoreEA.ICoreEAHander targetHandler, List <string> tableList
                                    , bool isShowUI = true)
        {
            bool ret = false;
            //Due to the old code use .ForEach way, it is hard to detect the error position and status
            //So use this boolean object to indicate whether there has error in the for each cycle .
            //Is true,didn't show the UI error messgae when UI , just set this value to true .
            bool isHasError = false;

            if (tableList.Count < 1)
            {
                "DataTransferNoTableMsg".GetFromResourece().Show();
                return(false);
            }

            List <SyncResultArgs> result            = new List <SyncResultArgs>();
            List <string>         targetDbTableList = targetHandler.GetTableListInDatabase();
            PageSwitchProgressBar dp = null;

            if (isShowUI)
            {
                dp = PageSwitchProgressBar.X_BeginLoadingDialog();
            }
            try
            {
                foreach (string tableName in tableList)
                {
                    SyncResultArgs curStatus = new SyncResultArgs();

                    //Get target table schema info
                    BaseTableSchema tableSchmea = srcHandler.GetTableSchemaInfoObject(tableName);

                    try
                    {
                        //If table not existed ,then create it.
                        if (!targetDbTableList.Contains(tableName))
                        {
                            if (true)
                            {
                                switch (targetHandler.HostedType)
                                {
                                case CoreE.UsedDatabaseType.OleDb:
                                    throw new NotImplementedException();
                                    break;

                                case CoreE.UsedDatabaseType.SqlServer:
                                    targetHandler.CreateTable(tableSchmea);
                                    try
                                    {
                                        CommonUtil.ExchangeDataBetweenAnyDbs(srcHandler, targetHandler, tableName);
                                    }
                                    catch (Exception exee)
                                    {
                                        Debug.WriteLine(exee.Message);
                                        targetHandler.DeleteTable(tableSchmea.TableName);
                                    }
                                    break;

                                case CoreE.UsedDatabaseType.MySql:
                                    targetHandler.CreateTable(tableSchmea);
                                    try
                                    {
                                        CommonUtil.ExchangeDataBetweenAnyDbs(srcHandler, targetHandler, tableName);
                                    }
                                    catch (Exception exee)
                                    {
                                        Debug.WriteLine(exee.Message);
                                        targetHandler.DeleteTable(tableSchmea.TableName);
                                    }
                                    finally
                                    {
                                    }
                                    break;

                                case CoreE.UsedDatabaseType.SqlCE35:
                                    FilterTableSchemInfoWithSSCE(tableSchmea);
                                    targetHandler.CreateTable(tableSchmea);
                                    CommonUtil.CopyTable(srcHandler.GetConnection(), (SqlCeConnection)targetHandler.GetConnection(),
                                                         string.Format("Select * from {0}", srcHandler.GetMaskedTableName(tableName)), tableName);
                                    break;

                                case CoreE.UsedDatabaseType.Sqlite:
                                    targetHandler.CreateTable(tableSchmea);
                                    CommonUtil.ExchangeDataBetweenAnyDbs(srcHandler, targetHandler, tableName);
                                    break;

                                case CoreE.UsedDatabaseType.Firebird:
                                    throw new NotImplementedException();
                                    break;

                                case CoreE.UsedDatabaseType.CSV:
                                    targetHandler.CreateTable(tableSchmea);
                                    try
                                    {
                                        PreProcessCSV(srcHandler, targetHandler, tableName);
                                    }
                                    catch (Exception exee)
                                    {
                                        Debug.WriteLine(exee.Message);
                                        targetHandler.DeleteTable(tableSchmea.TableName);
                                    }
                                    break;

                                case CoreE.UsedDatabaseType.Excel:
                                    throw new NotImplementedException();

                                case CoreE.UsedDatabaseType.Oracle:
                                    throw new NotImplementedException();

                                default:
                                    break;
                                }
                            }

                            curStatus.ProcessStatus = true;
                            curStatus.TableName     = tableName;
                        }
                    }
                    catch (Exception sss)
                    {
                        curStatus.LastErrorMsg  = sss.Message;
                        curStatus.ProcessStatus = false;
                        curStatus.TableName     = tableName;
                        if (isShowUI)
                        {
                            sss.HandleMyException();
                        }
                        else
                        {
                            isHasError = true;
                        }
                        return(false);
                    }
                    finally
                    {
                        result.Add(curStatus);
                        butNext.Content   = "End";
                        butNext.IsEnabled = false;
                    }
                }
                ;
                if (isHasError)
                {
                    ret = false;
                }
                else
                {
                    ret = true;
                }
            }
            finally
            {
                if (isShowUI)
                {
                    if (!dp.IsDisposed)
                    {
                        dp.X_EndLoadingDialog();
                    }
                }
            }
            if (isShowUI)
            {
                if ("DataTransfer_ConfirmViewReport".GetFromResourece().Confirm())
                {
                    ShowSyncResult rShow = new ShowSyncResult(result);
                    rShow.ShowDialog();
                }

                Close();
            }

            return(ret);
        }
        private CoreEA.ICoreEAHander CreateHandler(UIElement curElement)
        {
            CoreEA.ICoreEAHander tempHandler = null;

            ISrcControl srcControl = (ISrcControl)curElement;

            try
            {
                #region Source Control Handler
                if (curElement.GetType() == typeof(SelectSourceDbFile_OleDB))
                {
                    SelectSourceDbFile_OleDB curUI = curElement as SelectSourceDbFile_OleDB;
                    switch (curUI.CurrentDbType)
                    {
                    case UsingOleDbType.Excel:
                        tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.Excel).X_Handler;
                        tempHandler.Open((LoginInfo_Excel)srcControl.X_Result);
                        break;

                    case UsingOleDbType.Access:
                        tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.OleDb).X_Handler;
                        tempHandler.Open((LoginInfo_Oledb)srcControl.X_Result);
                        break;

                    case UsingOleDbType.CSV:
                        tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.CSV).X_Handler;
                        tempHandler.Open((LoginInfo_CSV)srcControl.X_Result);
                        break;
                    }
                }
                else if (curElement.GetType() == typeof(SelectSqlServerSource))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer).X_Handler;
                    tempHandler.Open((LoginInfo_SqlServer)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectMySqlSource))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.MySql).X_Handler;
                    tempHandler.Open((LoginInfo_MySql)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectSqlite3DbFile))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.Sqlite).X_Handler;
                    tempHandler.Open((LoginInfo_Sqlite)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectSSCEFile))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
                    tempHandler.Open((LoginInfo_SSCE)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectEffiproz))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.Effiproz).X_Handler;
                    tempHandler.Open((LoginInfo_Effiproz)srcControl.X_Result);
                }
                #endregion

                #region TargetControlHandler
                //Here the type is the class name of TargetUI
                else if (curElement.GetType() == typeof(SelectTargetDb_SqlCe))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
                    LoginInfo_SSCE ceInfo = srcControl.X_Result as LoginInfo_SSCE;
                    ceInfo.MaxDbSize = 3000;

                    tempHandler.Open(ceInfo);
                }
                else if (curElement.GetType() == typeof(SelectSqlServerTarget))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer).X_Handler;
                    tempHandler.Open((LoginInfo_SqlServer)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectTargetSqliteDB))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.Sqlite).X_Handler;
                    tempHandler.Open((LoginInfo_Sqlite)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectTargetMySql))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.MySql).X_Handler;
                    tempHandler.Open((LoginInfo_MySql)srcControl.X_Result);
                }
                else if (curElement.GetType() == typeof(SelectTargetCSVFile))
                {
                    tempHandler = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.CSV).X_Handler;
                    LoginInfo_CSV tempLogInfo = srcControl.X_Result as LoginInfo_CSV;
                    tempLogInfo.Database += "1.csv";

                    tempHandler.Open(tempLogInfo);
                }
                #endregion
            }
            catch (Exception ee)
            {
                ee.HandleMyException();
            }

            Debug.Assert(tempHandler != null, "Handler can't be null");
            return(tempHandler);
        }
        private void butNext_Click(object sender, RoutedEventArgs e)
        {
            //Do data transfer if finished
            if (tab1.SelectedIndex == tab1.Items.Count - 1)
            {
                if (!"DataTransferConfrimMsg".GetFromResourece().Confirm())
                {
                    return;
                }

                //Filter system table
                List <string> tableList = new List <string>();

                listTablesList.SelectedItems.Cast <string>().ToList().ForEach(subItem =>
                {
                    tableList.Add(subItem);
                });

                if (tableList.Count < 1)
                {
                    "DataTransferNoTableMsg".GetFromResourece().Notify();
                    return;
                }

                DoExchangeData(srcHandler, targetHandler, tableList);
                return;
            }

            if (tab1.SelectedIndex == 2)
            {
                if (listTablesList.SelectedItems.Count == 0)
                {
                    "DataTransferSelectTableMsg".GetFromResourece().Notify();
                    return;
                }
            }

            #region Generate the Handler
            Debug.WriteLine(((StackPanel)((TabItem)tab1.SelectedItem).Content).Children[0].GetType());
            UIElement curElement = ((StackPanel)((TabItem)tab1.SelectedItem).Content).Children[0];
            if (curElement.GetType().BaseType == typeof(UserControl))
            {
                ISrcControl tempSrcControl = (ISrcControl)curElement;

                if (!tempSrcControl.X_CanForwardToNext)
                {
                    tempSrcControl.X_ShowErrorTips();
                    return;
                }
                else
                {
                    if (tempSrcControl.X_IsSourceHandler)
                    {
                        if ((srcHandler != null) && (srcHandler.IsOpened))
                        {
                            srcHandler.Close();
                        }
                        srcHandler = CreateHandler(curElement);
                        if (!srcHandler.IsOpened)
                        {
                            "ErrorMsg_CannotConnectToSource".GetFromResourece().Warning();
                            return;
                        }
                    }
                    else
                    {
                        if ((targetHandler != null) && (targetHandler.IsOpened))
                        {
                            targetHandler.Close();
                        }
                        targetHandler = CreateHandler(curElement);
                        if (!targetHandler.IsOpened)
                        {
                            "ErrorMsg_CannotConnectToTarget".GetFromResourece().Warning();
                            return;
                        }
                    }
                }
            }

            #endregion

            tab1.SelectedIndex = 1 + tab1.SelectedIndex;

            //Select Source
            if (tab1.SelectedIndex == 1)
            {
                CurSrcDbType = ((DbTypeWrapper)cmbSrcDbType.SelectedItem).MyType;

                srcDbFileContainer.Children.Clear();

                ISrcControl srcControl = SourceControlFactory.GetProcessingControl(CurSrcDbType, true);
                if (srcControl == null)
                {
                    "DataTransfer_NotSupportDBType".GetFromResourece().Notify();
                    return;
                }
                srcDbFileContainer.Children.Add((UserControl)srcControl);
            }

            if (tab1.SelectedIndex == 2)
            {
                listTablesList.DataContext = null;

                List <string> t = srcHandler.GetTableListInDatabase();


                t.Sort();
                listTablesList.DataContext = t;
            }

            //Select Target Type
            if (tab1.SelectedIndex == 4)
            {
                CurTargetDbType = ((DbTypeWrapper)cmbTargetDbType.SelectedItem).MyType;

                targetDbFileContainer.Children.Clear();

                ISrcControl targetControl = SourceControlFactory.GetProcessingControl(CurTargetDbType, false);
                if (targetControl == null)
                {
                    "DataTransfer_NotSupportDBType".GetFromResourece().Notify();
                    return;
                }
                targetDbFileContainer.Children.Add((UserControl)targetControl);
            }

            //No next page
            if (tab1.SelectedIndex == tab1.Items.Count - 1)
            {
                resultSrcFile.Text    = srcHandler.CurDatabase;
                resultTargetFile.Text = targetHandler.CurDatabase;
                butNext.Content       = "Start";
            }

            butBack.IsEnabled = true;
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="core"></param>
        /// <param name="targetCeDBFile"></param>
        /// <param name="prcessTableList"></param>
        /// <param name="isNeedCopyData"></param>
        /// <returns></returns>
        public static List <SyncResultArgs> SyncDataFromSqlServerToSSCE(CoreEA.ICoreEAHander srcEngine, string targetCeDBFile,
                                                                        List <string> prcessTableList, bool isNeedCopyData)
        {
            List <SyncResultArgs> resultInfo = null;

            if (!srcEngine.IsOpened)
            {
                throw new ArgumentException("Need opened core object");
            }

            ICoreEAHander destEngine = new CoreEA.CoreE(CoreE.UsedDatabaseType.SqlCE35).X_Handler;

            //IF the ce database not existed ,then create it .
            if (!File.Exists(targetCeDBFile))
            {
                if (!destEngine.CreateDatabase(new LoginInfo_SSCE()
                {
                    DbName = targetCeDBFile, Pwd = ""
                }))
                {
                    "ImportData_CreateSSCEFileFailure".GetFromResourece().Notify();
                    return(null);
                }
            }

            destEngine.Open(new LoginInfo_SSCE()
            {
                DbName = targetCeDBFile, Pwd = "", IsEncrypted = false, CurOpenMode = OpenMode.ReadWrite
            });

            try
            {
                List <string> tableList = srcEngine.GetTableListInDatabase();
                if (tableList.Count <= 0)
                {
                    "ImportData_NoTable".GetFromResourece().Notify();
                    return(null);
                }

                resultInfo = new List <SyncResultArgs>();

                foreach (string srcSqlServerTableName in tableList)
                {
                    if (prcessTableList.Count > 0)
                    {
                        //If not in the need process table list ,then do not process it .
                        if (!prcessTableList.Contains(srcSqlServerTableName))
                        {
                            continue;
                        }
                    }

                    string    sqlCeTableName = srcSqlServerTableName;
                    DataTable tempDs         = srcEngine.GetColumnInfoFromTable(srcSqlServerTableName);

                    DataTable tempTable = tempDs;

#if DEBUG
                    tempDs.WriteXml(GlobalDefine.MyGlobal.GlobalDebugFolder + "SourceSchema.xml");
#else
#endif

                    SyncResultArgs args = new SyncResultArgs();
                    args.TableName = srcSqlServerTableName;

                    //Start Generate the Create Sdf table command
                    string tempCreateTableCmd = string.Empty;
                    tempCreateTableCmd = String.Format("CREATE TABLE [{0}] ", sqlCeTableName);
                    string tempSechma = string.Empty;

                    for (int i = 0; i < tempTable.Rows.Count; i++)
                    {
                        Debug.WriteLine("Source Field Name ------>" + tempTable.Rows[i]["COLUMN_NAME"].ToString());

                        //获取每个字段的类型和长度,If null then Each Type Define the size themself
                        //in ParseSqlServerDbTypeToSqlCeDbType method
                        int?length = null;

                        string lenNode =
                            CoreEA.Utility.TypeConvertor.ParseSqlServerLengthNodeNameFromTypeName(
                                tempTable.Rows[i]["DATA_TYPE"].ToString()
                                );

                        if ((!string.IsNullOrEmpty(lenNode)) && ((tempTable.Rows[i][lenNode] != DBNull.Value)))
                        {
                            length = int.Parse(tempTable.Rows[i][lenNode].ToString());
                        }

                        //建上述结果转换成SSCE 类型和语法
                        string appendix = CoreEA.Utility.TypeConvertor.ParseSqlServerDbTypeToSqlCeDbType(tempTable.Rows[i]["DATA_TYPE"].ToString(), length);

                        tempSechma += String.Format("{0} {1},", tempTable.Rows[i]["COLUMN_NAME"].ToString(), appendix);
                    }

                    tempSechma         = tempSechma.Substring(0, tempSechma.Length - 1);
                    tempCreateTableCmd = String.Format("{0} ({1})", tempCreateTableCmd, tempSechma);

                    if (destEngine.DoExecuteNonQuery(tempCreateTableCmd) != -1)
                    {
                        args.LastErrorMsg  = "Can't Create Target Table";
                        args.ProcessStatus = false;

                        resultInfo.Add(args);
                        //如果出错,继续执行下一次转换
                        continue;
                    }

                    if (isNeedCopyData)
                    {
                        CommonUtil.CopyTable(srcEngine.GetConnection(), (SqlCeConnection)destEngine.GetConnection(),
                                             String.Format("select * from {0}", srcSqlServerTableName), sqlCeTableName);
                    }

                    args.ProcessStatus = true;

                    resultInfo.Add(args);
                }
            }
            catch (Exception ee)
            {
                ee.HandleMyException();

                //((SqlCeDatabase)destEngine.DbHandler).CloseSharedConnection();

                if (File.Exists(targetCeDBFile))
                {
                    File.Delete(targetCeDBFile);
                }
            }

            return(resultInfo);
        }