示例#1
0
        private static object Execute(IDbConnection conn, string typeName, string sql, bool ExecuteScalar, dynamic param = null, IDbTransaction transaction = null, int?commandTimeout = 30, CommandType?commandType = null)
#endif
        {
            DMSFrameException.ThrowIfNull(conn, sql);
            DateTime       time         = DateTime.Now;
            string         providerName = string.Empty;
            IDMSDbProfiler dbProfiler   = GetProfiler(out providerName);

            try
            {
                #region Execute
                IEnumerable multiExec = (object)param as IEnumerable;
                Identity    identity;
                CacheInfo   info = null;

                if (multiExec != null && !(multiExec is string))
                {
                    #region multiExec
                    bool isFirst = true;
                    int  total   = 0;
                    using (var cmd = SetupCommand(conn, typeName, transaction, sql, null, null, commandTimeout, commandType))
                    {
                        string masterSql = null;
                        foreach (var obj in multiExec)
                        {
                            if (isFirst)
                            {
                                masterSql = cmd.CommandText;
                                isFirst   = false;
                                identity  = new Identity(sql, cmd.CommandType, conn, null, obj.GetType(), null);
                                info      = CacheMapper.GetCacheInfo(identity);
                            }
                            else
                            {
                                cmd.CommandText = masterSql; // because we do magic replaces on "in" etc
                                cmd.Parameters.Clear();      // current code is Add-tastic
                            }
                            info.ParamReader(cmd, obj);
                            time = DateTime.Now;

                            try
                            {
                                dbProfiler.ExecuteStart(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery);
                                total += cmd.ExecuteNonQuery();
                                dbProfiler.ExecuteFinish(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery, (DateTime.Now - time).TotalMilliseconds, null);
                            }
                            catch (Exception exp)
                            {
                                dbProfiler.OnError(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery, (DateTime.Now - time).TotalMilliseconds, exp);
                            }
                        }
                    }
                    #endregion
                    return(total);
                }
                // nice and simple
                if ((object)param != null)
                {
                    identity = new Identity(sql, commandType, conn, null, (object)param == null ? null : ((object)param).GetType(), null);
                    info     = CacheMapper.GetCacheInfo(identity);
                }
                return(ExecuteCommand(conn, typeName, transaction, sql, ExecuteScalar, (object)param == null ? null : info.ParamReader, (object)param, commandTimeout, commandType));

                #endregion
            }
            catch (Exception ex)
            {
                Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), string.Format("msg:{0},sql:{1},param:{2}", ex.Message, sql, param), ex);
                throw ex;
            }
        }