Example #1
0
        public static DataTable BulkGetEmptyDataTable(this DbAccessor db, string tableName)
        {
            DataTable dt = db.QueryDataTable(string.Format("select * from {0} where 1=2", tableName));

            dt.TableName = tableName;
            return(dt);
        }
Example #2
0
        public static DbAccessor NewDbAccessor(string connectionString, string providerName)
        {
            DbAccessor dba = null;

            if (string.IsNullOrEmpty(providerName) ||
                providerName == "System.Data.SqlClient" ||
                providerName.ToLower() == DbProviderName.SqlServer.ToString().ToLower())
            {
                dba = new SqlServerAccessor(connectionString);
            }
            else if (providerName.ToLower() == DbProviderName.MySql.ToString().ToLower() ||
                     providerName == "MySql.Data.MySqlClient")
            {
                dba = new MySqlAccessor(connectionString);
            }
            else
            {
                Type tProviderName = Type.GetType(providerName);
                if (tProviderName == null)
                {
                    throw new Exception(string.Format("ConnectionString({0})的ProviderName({1})找不到该类型!", connectionString, providerName));
                }
                //else if (!(tProviderName.IsSubclassOf(typeof(DbAccessor))))
                //{
                //    throw new Exception(string.Format("ConnectionString({0})的ProviderName({1})该类型不是DbAccessor的实现类!", connectionString, providerName));
                //}
                object oProviderName = Activator.CreateInstance(tProviderName, connectionString);
                dba = oProviderName as DbAccessor;
            }

            return(dba);
        }
        public static int Delete <T>(this DbAccessor db, object objIdParameters)
        {
            var         tableMapping = new TableMapping(typeof(T));
            ISqlBuilder sqlBuilder   = new DefaultSqlBuilder(tableMapping);

            #region 处理参数为基础类型,即直接为主键的值
            if (objIdParameters.GetType() == typeof(string) || objIdParameters.GetType().IsPrimitive)
            {
                var dicParam = new Dictionary <string, object>();

                var keyColumns = tableMapping.GetKeyColumns();

                if (keyColumns.Count == 0)
                {
                    throw new Exceptions.EntityException("实体没有设置主键!");
                }

                string keyColumnName = keyColumns[0].ColumnName;

                dicParam.Add(keyColumnName, objIdParameters);
                objIdParameters = dicParam;
            }
            #endregion

            return(db.ExecuteNonQuery(sqlBuilder.BuildDelete(), objIdParameters));
        }
        public static IList <T> SelectList <T>(this DbAccessor db, object objParameters)
        {
            var         tableMapping = new TableMapping(typeof(T));
            ISqlBuilder sqlBuilder   = new DefaultSqlBuilder(tableMapping);

            return(db.QueryEntities <T>(sqlBuilder.BuildSelectList(), objParameters));
        }
        public static void BulkWriteToServer(this DbAccessor db, string tableName, DbDataReader reader, int batchSize = 0, params BulkCopyColumnMapping[] columnMappings)
        {
            DbBulkCopy dbBulkCopy = DbBulkCopyFactory.GetDbBulkCopy(db, db.ConnectionString);

            dbBulkCopy.BulkCopyTimeout = db.CommandTimeout;
            dbBulkCopy.DbTransaction   = db.DbTransaction;
            dbBulkCopy.WriteToServer(tableName, reader, batchSize, columnMappings);
        }
        public static int Insert <T>(this DbAccessor db, object objParameters)
        {
            var mapping = new TableMapping(typeof(T));

            mapping.FilterColumn(objParameters);
            ISqlBuilder sqlBuilder = new DefaultSqlBuilder(mapping);

            return(db.ExecuteNonQuery(sqlBuilder.BuildInsert(), objParameters));
        }
Example #7
0
 public static DbAccessor GetDbAccessor()
 {
     if (_dba == null)
     {
         lock (_objLock)
         {
             if (_dba == null)
             {
                 _dba             = CreateDbAccessor();
                 _dba.IsSingleton = true;
             }
         }
     }
     return(_dba);
 }
        public static int UpdateFields(this DbAccessor db, object objEntity, params string[] inclusiveFields)
        {
            List <string> lsInclusiveFields = new List <string>();

            lsInclusiveFields.AddRange(inclusiveFields);
            var mapping = new TableMapping(objEntity.GetType());

            foreach (var item in mapping.GetKeyColumns())
            {
                lsInclusiveFields.Add(item.ColumnName);
            }
            mapping.FilterColumn(lsInclusiveFields.ToArray());
            ISqlBuilder sqlBuilder = new DefaultSqlBuilder(mapping);

            return(db.ExecuteNonQuery(sqlBuilder.BuildUpdate(), objEntity));
        }
Example #9
0
        public static DbAccessor CreateDbAccessor(string connectionString, string providerName)
        {
            DbAccessor dba = NewDbAccessor(connectionString, providerName);

            if (dba != null)
            {
                IEnumerable <IDbExecuteHandler> dbExecuteHandlers = DbExecuteHandlerFactory.GetDbExecuteHandlers();

                foreach (IDbExecuteHandler handler in dbExecuteHandlers)
                {
                    dba.PreCommandExecute += handler.HandleExecute;
                }
            }

            return(dba);
        }
Example #10
0
        public static IList <T> PageQueryEntities <T>(this DbAccessor db,
                                                      string sql, string orderClause, int pageIndex, int rowCount, out int totalRowCount,
                                                      object objParameters = null)
        {
            DbPaging paging = DbPagingFactory.GetDbPaging(db);

            if (objParameters == null)
            {
                object objTotalRowCount = db.ExecuteScalar <object>(paging.GetCountSql(sql));
                totalRowCount = Convert.ToInt32(objTotalRowCount);
                int startRowIndex = 1 + (pageIndex - 1) * rowCount;
                return(db.QueryEntities <T>(paging.GetPageSql(sql, orderClause, startRowIndex, rowCount)));
            }
            else
            {
                object objTotalRowCount = db.ExecuteScalar <object>(paging.GetCountSql(sql), objParameters);
                totalRowCount = Convert.ToInt32(objTotalRowCount);
                int startRowIndex = 1 + (pageIndex - 1) * rowCount;
                return(db.QueryEntities <T>(paging.GetPageSql(sql, orderClause, startRowIndex, rowCount), objParameters));
            }
        }
        public static int Update(this DbAccessor db, object objEntity)
        {
            ISqlBuilder sqlBuilder = new DefaultSqlBuilder(new TableMapping(objEntity.GetType()));

            return(db.ExecuteNonQuery(sqlBuilder.BuildUpdate(), objEntity));
        }
Example #12
0
        public static T Select <T>(this DbAccessor db, object objIdParameters)
        {
            ISqlBuilder sqlBuilder = new DefaultSqlBuilder(new TableMapping(typeof(T)));

            return(db.QueryEntity <T>(sqlBuilder.BuildSelect(), objIdParameters));
        }
Example #13
0
 public DbAccessorContext(DbAccessor dbAccessor, DbCommand dbCommand)
 {
     DbCommand  = dbCommand;
     DbAccessor = dbAccessor;
 }