Beispiel #1
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);
        }
Beispiel #2
0
        /// <summary>
        /// mysql to sqlce
        /// </summary>
        /// <param name="server"></param>
        /// <param name="database"></param>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <param name="sdfFile"></param>
        /// <param name="isCopyData"></param>
        /// <returns></returns>
        public static bool SyncDataFromMysqlToSqlCE(string server, string database, string username, string pwd, string sdfFile, bool isCopyData)
        {
            bool result = false;

            if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(sdfFile))
            {
                throw new Exception("Not valid parameters");
            }

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

            if (!File.Exists(sdfFile))
            {
                if (!targetEnginer.CreateDatabase(new LoginInfo_SSCE()
                {
                    DbName = sdfFile
                }))
                {
                    "ImportData_CreateSSCEFileFailure".GetFromResourece().Notify();
                    return(false);
                }
            }

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

            ICoreEAHander srcEngineer = new CoreEA.CoreE(CoreE.UsedDatabaseType.MySql).X_Handler;

            try
            {
                srcEngineer.Open(new LoginInfo_MySql()
                {
                    Server = server, Database = database, Username = username, Pwd = pwd
                });

                List <string> tableList = srcEngineer.GetTableListInDatabase(database);

                if (tableList.Count <= 0)
                {
                    "ImportData_NoTable".GetFromResourece().Notify();
                    return(false);
                }
                List <string> targetDbList = targetEnginer.GetTableListInDatabase();

                foreach (string tableName in tableList)
                {
                    //Don't import table which name has existed.
                    if (targetDbList.Contains(tableName))
                    {
                        continue;
                    }
                    string sqlCeTableName = tableName;


                    DataTable tempTable = srcEngineer.GetColumnInfoFromTable(tableName);

                    //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());
                        tempSechma += String.Format("{0} {1},", tempTable.Rows[i]["COLUMN_NAME"].ToString(),
                                                    CoreEA.Utility.TypeConvertor.ParseMySqlDbTypeToSqlCeDbType(tempTable.Rows[i]["COLUMN_TYPE"].ToString()));
                    }

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

                    if (targetEnginer.DoExecuteNonQuery(tempCreateTableCmd) != -1)
                    {
                        throw new Exception(string.Format("Create table {0} error", tableName));
                    }

                    if (isCopyData)
                    {
                        CommonUtil.CopyTable(srcEngineer.GetConnection(),
                                             (SqlCeConnection)targetEnginer.GetConnection(),
                                             String.Format("select * from `{0}`", tableName), sqlCeTableName);
                    }

                    result = true;
                }
            }
            catch (Exception ee)
            {
                ee.HandleMyException();
                //((SqlCeDatabase)targetEnginer.DbHandler).CloseSharedConnection();
                if (File.Exists(sdfFile))
                {
                    File.Delete(sdfFile);
                }
            }

            return(result);
        }