/// <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); }
/// <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); }