Ejemplo n.º 1
0
        private static void SynLocalCacheByCacheConfig(DataTable dtCacheTable)
        {
            string        strTableName  = dtCacheTable.Rows[0]["TableName"].ToString();
            string        strId         = dtCacheTable.Rows[0]["Id"].ToString();
            string        strConn       = dtCacheTable.Rows[0]["ConnectionString"].ToString();
            DatabaseTable databaseTable = CacheManager.GetDatabaseTable(strConn, SqlType.SqlServer, strTableName);

            if (databaseTable != null)
            {
                string strDdl = CacheManager.RunTableDdl(databaseTable, SqlType.SQLite);
                strDdl += "; SELECT 'ok'";
                IDbHelper helper = GlobalHelp.GetDataAccessSqliteHelper();
                helper.CreateCommand("DROP Table IF EXISTS [" + strTableName + "]");
                helper.ExecuteNonQuery();
                helper.CreateCommand(strDdl);
                DataTable dtCache = helper.ExecuteQuery();
                if (dtCache.Rows.Count > 0 && dtCache.Rows[0][0].ToString().Trim() == "ok")
                {
                    //删除本地缓存设置表数据
                    helper.CreateCommand("DELETE FROM CacheConfig WHERE TableName ='" + strTableName + "' ");
                    helper.ExecuteNonQuery();
                    //获取服务器缓存表数据,并同步至本地缓存数据库
                    IDbHelper helperSql = new MSSQLHelper(strConn);
                    helperSql.CreateCommand("SELECT * FROM " + strTableName + " with(nolock)");
                    DataTable dtBatch = helperSql.ExecuteQuery();
                    dtBatch.TableName = strTableName;
                    for (int i = 0; i < dtBatch.Columns.Count; i++)
                    {
                        dtBatch.Columns[i].AutoIncrement = false;
                    }
                    helper.BatchInsert(dtBatch);
                    //同步本地缓存设置表数据
                    dtCacheTable.TableName = "CacheConfig";
                    for (int i = 0; i < dtCacheTable.Columns.Count; i++)
                    {
                        dtCacheTable.Columns[i].AutoIncrement = false;
                    }
                    for (int i = 0; i < dtCacheTable.Rows.Count; i++)
                    {
                        dtCacheTable.Rows[i]["ConnectionString"] = string.Empty;
                    }
                    helper.BatchInsert(dtCacheTable);
                    if (!CacheManager.CacheDictionary.ContainsKey(strTableName))
                    {
                        CacheManager.CacheDictionary.Add(strTableName, strTableName);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>同步缓存
        ///
        /// </summary>
        public static void SynCache()
        {
            IDbHelper helperSqlServer = GlobalHelp.GetDataAccessHelper();
            IDbHelper helperSqlite    = GlobalHelp.GetDataAccessSqliteHelper();
            string    strSql          = "Select * from CacheConfig";
            string    strIds          = GetLocalCacheIds();

            if (strIds != string.Empty)
            {
                strSql = strSql + " where Id  not in(" + strIds + ") ";
            }
            helperSqlServer.CreateCommand(strSql);
            DataTable dtCacheTable = helperSqlServer.ExecuteQuery();

            if (dtCacheTable.Rows.Count <= 0)
            {
                return;
            }
            foreach (DataRow dr in dtCacheTable.Rows)
            {
                string        strTableName  = dr["TableName"].ToString();
                string        strId         = dr["Id"].ToString();
                string        strConn       = dr["ConnectionString"].ToString();
                DatabaseTable databaseTable = GetDatabaseTable(strConn, SqlType.SqlServer, strTableName);
                if (databaseTable != null)
                {
                    #region 创建本地脚本
                    string strDdl = RunTableDdl(databaseTable, SqlType.SQLite);
                    strDdl += "; SELECT 'ok'";
                    helperSqlite.CreateCommand("DROP Table IF EXISTS [" + strTableName + "]");
                    helperSqlite.ExecuteNonQuery();
                    helperSqlite.CreateCommand(strDdl);
                    DataTable dtCache = helperSqlite.ExecuteQuery();
                    #endregion
                    if (dtCache.Rows.Count > 0 && dtCache.Rows[0][0].ToString().Trim() == "ok")
                    {
                        //删除本地缓存设置表数据
                        helperSqlite.CreateCommand("DELETE FROM CacheConfig WHERE TableName ='" + strTableName + "' ");
                        helperSqlite.ExecuteNonQuery();
                        //获取服务器缓存表数据,并同步至本地缓存数据库
                        IDbHelper helperTemp = new MSSQLHelper(strConn);
                        helperTemp.CreateCommand("SELECT * FROM " + strTableName + " with(nolock)");
                        DataTable dtBatch = helperTemp.ExecuteQuery();
                        dtBatch.TableName = strTableName;
                        for (int i = 0; i < dtBatch.Columns.Count; i++)
                        {
                            dtBatch.Columns[i].AutoIncrement = false;
                        }
                        helperSqlite.BatchInsert(dtBatch);
                        //同步本地缓存设置表数据
                        dtCacheTable.TableName = "CacheConfig";
                        for (int i = 0; i < dtCacheTable.Columns.Count; i++)
                        {
                            dtCacheTable.Columns[i].AutoIncrement = false;
                        }
                        for (int i = 0; i < dtCacheTable.Rows.Count; i++)
                        {
                            dtCacheTable.Rows[i]["ConnectionString"] = string.Empty;
                        }
                        helperSqlite.BatchInsert(dtCacheTable);
                    }
                }
            }
        }