Ejemplo n.º 1
0
        public static DataTable GetTable(string strSql, object param = null, IDbTransaction trans = null, string connectString = "")
        {
            string result = "成功";

            IDbConnection dbConnection = AppDataBase.GetDbConnection(connectString);

            DataTable dataTable = new DataTable();

            try
            {
                using (IDataReader dataReader = dbConnection.ExecuteReader(strSql, param, trans))
                {
                    dataTable.Load(dataReader);

                    AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, result, param);

                    return(dataTable);
                }
            }
            catch (Exception ex)
            {
                AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, $"失败:{ex.Message}", param);
            }

            return(dataTable);
        }
Ejemplo n.º 2
0
        public static Object ExecuteScalar(string sql, IDbConnection dbConnection = null, object param = null, IDbTransaction trans = null)
        {
            string result = "成功";

            if (dbConnection == null)
            {
                dbConnection = AppDataBase.GetDbConnection(ConnStrings);
            }
            //dbConnection.Open();
            //trans = dbConnection.BeginTransaction();

            try
            {
                object obj = dbConnection.ExecuteScalar(sql, param, trans, CommandTimeout, CommandType.Text);
                AppLog.WriteDbLog(dbConnection.ConnectionString, sql, result, param);

                //trans.Commit();
                return(obj == null ? "" : obj);
            }
            catch (Exception ex)
            {
                //trans.Rollback();
                AppLog.WriteDbLog(dbConnection.ConnectionString, sql, $"失败:{ex.Message}", param);
                throw ex;
            }
        }
Ejemplo n.º 3
0
        public static Tuple <IEnumerable <T1>, IEnumerable <T2> > Query <T1, T2>(string sql, object param)
        {
            string result = "成功";

            IDbConnection dbConnection = AppDataBase.GetDbConnection(ConnStrings);

            IEnumerable <T1> enumerable = null;

            IEnumerable <T2> enumerable2 = null;

            bool flag = !string.IsNullOrEmpty(sql);

            if (flag)
            {
                try
                {
                    using (SqlMapper.GridReader gridReader = dbConnection.QueryMultiple(sql, param, null, CommandTimeout, CommandType.Text))
                    {
                        enumerable = gridReader.Read <T1>(true);

                        enumerable2 = gridReader.Read <T2>(true);

                        AppLog.WriteDbLog(dbConnection.ConnectionString, sql, result, param);
                    }
                }
                catch (Exception ex)
                {
                    AppLog.WriteDbLog(dbConnection.ConnectionString, sql, $"失败:{ex.Message}", param);
                }
            }

            return(Tuple.Create <IEnumerable <T1>, IEnumerable <T2> >(enumerable, enumerable2));
        }
Ejemplo n.º 4
0
        public static IEnumerable <T> Query <T>(string strSql, object param = null, IDbTransaction trans = null, IDbConnection dbConnection = null)
        {
            string result = "成功";

            if (dbConnection == null)
            {
                dbConnection = AppDataBase.GetDbConnection(ConnStrings);
            }

            IEnumerable <T> result2;

            try
            {
                IEnumerable <T> enumerable = dbConnection.Query <T>(strSql, param, trans, Buffered, CommandTimeout, CommandType.Text);

                AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, result, param);

                result2 = enumerable;

                return(result2);
            }
            catch (Exception ex)
            {
                AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, $"失败:{ex.Message}", param);
            }

            return(result2 = null);
        }
Ejemplo n.º 5
0
        public static DataSet GetDataSet(string strSql, object param = null, IDbTransaction trans = null, string connectString = "")
        {
            IDbConnection dbConnection = AppDataBase.GetDbConnection(connectString);

            DataSet dataSet = new DataSet();

            using (IDataReader dataReader = dbConnection.ExecuteReader(strSql, param, trans))
            {
                dataSet.Load(dataReader, LoadOption.OverwriteChanges, null, new DataTable[0]);
            }

            return(dataSet);
        }
Ejemplo n.º 6
0
 public static object QueryStoredProcedure(string strSql, object param, IDbTransaction trans = null, IDbConnection dbConnection = null)
 {
     if (dbConnection == null)
     {
         dbConnection = AppDataBase.GetDbConnection(ConnStrings);
     }
     try
     {
         dbConnection.Query(strSql, param, null, Buffered, null, CommandType.StoredProcedure).FirstOrDefault();
         AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, "成功", param);
     }
     catch (Exception e)
     {
         AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, $"失败:{e.Message}", param);
     }
     return(param);
 }
Ejemplo n.º 7
0
        public static T QuerySingle <T>(string strSql, object param = null, IDbTransaction trans = null, IDbConnection dbConnection = null)
        {
            string result = "成功";

            if (dbConnection == null)
            {
                dbConnection = AppDataBase.GetDbConnection(ConnStrings);
            }

            try
            {
                IEnumerable <T> enumerable = dbConnection.Query <T>(strSql, param, trans, Buffered, CommandTimeout, CommandType.Text);

                if (enumerable != null)
                {
                    AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, result, param);

                    return(enumerable.FirstOrDefault());
                }
                else
                {
                    AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, $"失败:{enumerable}未找到数据!", param);

                    return(default(T));
                }

                //T result2 = dbConnection.QuerySingle<T>(strSql, param, trans, CommandTimeout, CommandType.Text);

                //AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, result, param);

                //return result2;
            }
            catch (Exception ex)
            {
                AppLog.WriteDbLog(dbConnection.ConnectionString, strSql, $"失败:{ex.Message}", param);

                return(default(T));
            }
        }
Ejemplo n.º 8
0
        public static IEnumerable <T> QueryStored <T>(string storedName, object param)
        {
            string result = "成功";

            IDbConnection dbConnection = AppDataBase.GetDbConnection(ConnStrings);

            IEnumerable <T> result2 = null;

            try
            {
                bool flag = !string.IsNullOrEmpty(storedName);

                if (flag)
                {
                    bool flag2 = param != null;

                    if (flag2)
                    {
                        result2 = dbConnection.Query <T>(storedName, param, null, Buffered, CommandTimeout, CommandType.StoredProcedure);
                    }
                    else
                    {
                        result2 = dbConnection.Query <T>(storedName, null, null, Buffered, CommandTimeout, CommandType.StoredProcedure);
                    }

                    AppLog.WriteDbLog(dbConnection.ConnectionString, storedName, result, param);
                }

                return(result2);
            }
            catch (Exception ex)
            {
                AppLog.WriteDbLog(dbConnection.ConnectionString, storedName, $"失败:{ex.Message}", param);
            }

            return(result2);
        }
Ejemplo n.º 9
0
        public static int Execute(string sql, object param = null, IDbTransaction trans = null, IDbConnection dbConnection = null)
        {
            string result = "成功";

            if (dbConnection == null)
            {
                dbConnection = AppDataBase.GetDbConnection(ConnStrings);
            }

            try
            {
                int num = dbConnection.Execute(sql, param, trans, CommandTimeout, CommandType.Text);

                AppLog.WriteDbLog(dbConnection.ConnectionString, sql, result, param);

                return(num);
            }
            catch (Exception ex)
            {
                AppLog.WriteDbLog(dbConnection.ConnectionString, sql, $"失败:{ex.Message}", param);
                throw ex;
            }
            //return 0;
        }