private void SyncSqlServer2SSCE(object sender, RoutedEventArgs e) { string server = sqlServerLoginControl1.X_Result.Server; string database = sqlServerLoginControl1.X_Result.DbName; string username = sqlServerLoginControl1.X_Result.UID; string pwd = sqlServerLoginControl1.X_Result.PWD; if (core == null) { "ImportNeedSelectTables".GetFromResourece().Notify(); return; } if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database) || string.IsNullOrEmpty(username)) { "ImportEachElement".GetFromResourece().Notify(); return; } string sdfFile = txtTargetFile.Text; PageSwitchProgressBar dp = PageSwitchProgressBar.X_BeginLoadingDialog(); List <SyncResultArgs> result = null; try { List <string> proecssTableList = new List <string>(); foreach (string item in tableList.SelectedItems) { proecssTableList.Add(item); } result = CommonUtil.SyncDataFromSqlServerToSSCE(core, sdfFile, proecssTableList, (bool)chkIsNeedCopyData.IsChecked); } catch (Exception ee) { ee.HandleMyException(); } finally { if (!dp.IsDisposed) { dp.X_EndLoadingDialog(); } } if (result != null) { ShowSyncResult rShow = new ShowSyncResult(result); #if DEBUG #else rShow.Owner = this; #endif rShow.ShowDialog(); } }
/// <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); }