Esempio n. 1
0
        /// <summary>
        /// 返回一个ResultSet  查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="OrderBy"></param>
        /// <param name="pl"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public ResultSet GetResultSet(string sql, string OrderBy, ParamList pl, IDbTransaction tran = null)
        {
            DBAdapter adapter = GetDBAdapter();

            try
            {
                var conn = adapter.OpenDatabase();
                if (pl.ispage)
                {
                    string pagesql  = adapter.GetPageSql(sql, OrderBy);
                    string countsql = adapter.GetCountSql(sql);

                    var m = conn.Query(pagesql, pl.Parameters, tran);
                    var c = conn.ExecuteScalar <int>(countsql, pl.Parameters, tran);
                    return(ResultSet.GetResultSet(m, c));
                }
                else
                {
                    var m = conn.Query(sql + " order by " + OrderBy, pl.Parameters, tran);
                    return(ResultSet.GetResultSet(m));
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                adapter.CloseDatabase();
            }
        }
Esempio n. 2
0
        public DataTable GetDataTable(string sql, IDbDataParameter[] parlist = null, IDbTransaction tran = null)
        {
            DBAdapter adapter = null;;

            try
            {
                if (tran == null)
                {
                    adapter = GetDBAdapter();
                }
                var conn = tran != null ? tran.Connection : adapter.OpenDatabase();


                using (IDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    if (tran != null)
                    {
                        cmd.Transaction = tran;
                    }
                    if (parlist != null)
                    {
                        foreach (var par in parlist)
                        {
                            cmd.Parameters.Add(par);
                        }
                    }
                    var     ida = adapter.GetDataAdapter(cmd);
                    DataSet ds  = new DataSet();
                    ida.Fill(ds);
                    return(ds.Tables[0]);
                }
            }
            catch (Exception ex) { throw ex; }
            finally
            {
                if (adapter != null)
                {
                    adapter.CloseDatabase();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 同一个sql语句,批量操作多个对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="models"></param>
        /// <param name="tran"></param>
        public int Execute(string sql, IEnumerable <object> models, IDbTransaction tran = null)
        {
            DBAdapter adapter = null;;

            try
            {
                if (tran == null)
                {
                    adapter = GetDBAdapter();
                }
                var conn = tran != null ? tran.Connection : adapter.OpenDatabase();
                return(conn.Execute(sql, models, tran));
            }
            catch (Exception ex) { throw ex; }
            finally
            {
                if (adapter != null)
                {
                    adapter.CloseDatabase();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 执行sql,返回受影响条记录数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pl"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public int Execute(string sql, ParamList pl, IDbTransaction tran = null)
        {
            DBAdapter adapter = null;;

            try
            {
                if (tran == null)
                {
                    adapter = GetDBAdapter();
                }
                var conn = tran != null ? tran.Connection : adapter.OpenDatabase();
                return(conn.Execute(sql, pl.Parameters, tran));
            }
            catch { throw; }
            finally
            {
                if (adapter != null)
                {
                    adapter.CloseDatabase();
                }
            }
        }
Esempio n. 5
0
        public List <T> Query <T>(string sql, object query = null, IDbTransaction tran = null)
        {
            DBAdapter adapter = null;;

            try
            {
                if (tran == null)
                {
                    adapter = GetDBAdapter();
                }
                var conn = tran != null ? tran.Connection : adapter.OpenDatabase();
                var m    = conn.Query <T>(sql, query).ToList <T>();
                return(m);
            }
            catch (Exception ex) { throw ex; }
            finally
            {
                if (adapter != null)
                {
                    adapter.CloseDatabase();
                }
            }
        }