Esempio n. 1
0
        private void InitTable(string tableName)
        {
            TableName = tableName;
            string CacheKey = MyConnectionString + ":TBL:" + TableName;

            if (!DatabaseCache.Instance.ContainsKey(CacheKey + ":Insert"))
            {
                lock (DatabaseCache.Instance)
                {
                    if (!DatabaseCache.Instance.ContainsKey(CacheKey + ":Insert"))
                    {
                        using (IDbConnection conn = CreateConnection())
                        {
                            using (IDbCommand command = CreateCommand(conn, MyTransaction))
                            {
                                IDbDataAdapter adapter = MyDatabase.CreateAdapter(command);

                                try
                                {
                                    DatabaseCache insertCache, updateCache, deleteCache, isExistCache;
                                    DataTable     TableSchema;

                                    MyDatabase.ExtractTableParameters(TableName,
                                                                      adapter,
                                                                      out insertCache,
                                                                      out deleteCache,
                                                                      out updateCache,
                                                                      out isExistCache,
                                                                      out TableSchema);

                                    DatabaseCache.Instance.Add(CacheKey + ":Insert", insertCache);
                                    DatabaseCache.Instance.Add(CacheKey + ":Update", updateCache);
                                    DatabaseCache.Instance.Add(CacheKey + ":Delete", deleteCache);
                                    DatabaseCache.Instance.Add(CacheKey + ":IsExist", isExistCache);
                                }
                                finally
                                {
                                    if (adapter is IDisposable)
                                    {
                                        (adapter as IDisposable).Dispose();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            InsertCache  = DatabaseCache.Instance[CacheKey + ":Insert"];
            UpdateCache  = DatabaseCache.Instance[CacheKey + ":Update"];
            DeleteCache  = DatabaseCache.Instance[CacheKey + ":Delete"];
            IsExistCache = DatabaseCache.Instance[CacheKey + ":IsExist"];
        }
Esempio n. 2
0
        /// <summary>
        /// 从SQL语句中解析参数, 加入cmd.Parameter中
        /// </summary>
        /// <param name="values"></param>
        /// <param name="SQL"></param>
        /// <param name="cmd"></param>
        internal void BuildCommandText(IList values, string SQL, IDbCommand cmd)
        {
            if (values != null && values.Count > 0 && values[0] is IList)
            {
                BuildCommandText(values[0] as IList, SQL, cmd);
                return;
            }

            string CacheKey = MyConnectionString + ":" + SQL;

            cmd.CommandType = CommandType.Text;

            //不带任何参数无需缓存
            if (reg.IsMatch(SQL) == false)
            {
                cmd.CommandText = SQL;
                return;
            }

            if (!DatabaseCache.Instance.ContainsKey(CacheKey))
            {
                lock (DatabaseCache.Instance)
                {
                    if (!DatabaseCache.Instance.ContainsKey(CacheKey))
                    {
                        StringBuilder sbSQL = new StringBuilder(SQL);

                        IDbDataParameter[] pList = new IDbDataParameter[values.Count];

                        for (int i = 0, j = values.Count; i < j; i++)
                        {
                            pList[i] = cmd.CreateParameter();
                            pList[i].ParameterName = MyDatabase.FormaterParameterName(i);
                            pList[i].Value         = null;

                            sbSQL.Replace("{" + i + "}", MyDatabase.FormaterParameterName(i));
                        }

                        DatabaseCache.Instance.Add(
                            CacheKey,
                            new DatabaseCache(sbSQL.ToString(), pList));

                        sbSQL = null;
                    }
                }
            }

            DatabaseCache cache = DatabaseCache.Instance[CacheKey];

            cmd.CommandText = cache.SQL;

            AddParametersClone2Cmd(cmd, cache.Parameters, values);
        }
Esempio n. 3
0
        /// <summary>
        /// 解析存储过程参数
        /// </summary>
        /// <param name="ProcedureName">存储过程名称</param>
        /// <param name="objs">如果传入值,则按照参数的Input类型,顺序赋值到Parameter中</param>
        /// <returns>IDataParameter数组</returns>
        protected IDbDataParameter[] GetProcedureParameters(string ProcedureName, params object[] objs)
        {
            string CacheKey = MyConnectionString + ":SP:" + ProcedureName;

            if (!DatabaseCache.Instance.ContainsKey(CacheKey))
            {
                lock (DatabaseCache.Instance)
                {
                    if (!DatabaseCache.Instance.ContainsKey(CacheKey))
                    {
                        IDbDataParameter[] ParaList = ExtractProcedureParameters(ProcedureName);

                        DatabaseCache MyCache = new DatabaseCache(ProcedureName, ParaList);

                        DatabaseCache.Instance.Add(CacheKey, MyCache);
                    }
                }
            }

            return(CloneParameters(DatabaseCache.Instance[CacheKey].Parameters, objs));
        }