コード例 #1
0
ファイル: DataSource.cs プロジェクト: lishuo3331/dbhelper
        private DataSourceFactory(string name)
        {
            name = name.ToLower();
            DataSourceConfig dsConfig = DBHelperFactory2.DataSourceMap[name];

            this.m_dataSourceName   = dsConfig.dataSourceName;
            this.m_connectionString = dsConfig.connStr;
            this.m_providerName     = dsConfig.providerName;
            string dbDialectClass = dsConfig.dialectClass;

            m_dbDialect = (IDbDialect)ToolKit.CreateInstance(dbDialectClass);

            //默认配置
            this.m_minSize = 50;
            this.m_maxSize = 100;
            this.m_usePool = true;
            this.m_timeOut = 1000;
            try { this.m_minSize = dsConfig.minSize; }
            catch { }
            try { this.m_maxSize = dsConfig.maxSize; }
            catch { }
            try { this.m_timeOut = dsConfig.timeOut; }
            catch { }
            try { this.m_usePool = dsConfig.pooled; }
            catch { }
        }
コード例 #2
0
        /// <summary>
        /// 实例化数据库访问对象
        /// </summary>
        /// <param name="type"></param>
        /// <param name="connectionString"></param>
        public DataBaseAccess(DataBaseType type, string connectionString)
        {
            if (connectionString.IndexOf("$ROOT$") != -1)
            {
                connectionString = connectionString.Replace("$ROOT$",
                                                            EnvUtil.GetBaseDirectory());
            }

            this.DbType = type;

            switch (type)
            {
            case DataBaseType.OLEDB:
                dbDialect = new OleDbFactory(connectionString);
                break;

            case DataBaseType.SQLite:
                dbDialect = new SQLiteFactory(connectionString);
                break;

            case DataBaseType.MonoSQLite:
                throw new Exception("not implement on .net standard");

            //    dbDialect = new MonoSQLiteFactory(connectionString);
            case DataBaseType.SQLServer:
                dbDialect = new SqlServerFactory(connectionString);
                break;

            case DataBaseType.MySQL:
                dbDialect = new MySqlFactory(connectionString);
                break;
            }
        }
コード例 #3
0
 public DBHelperImpl(string dsName)
 {
     this.m_dataSourceName = dsName;
     //EbfDataSource dataSource = EbfDataSource.GetInstanceOf(dsName);
     //this.m_dbDialect = dataSource.DbDialect;
     this.m_dbDialect    = DataSourceFactory.GetInstance(dsName).DbDialect;
     this.m_dbConnection = this.CreateDbConnection(dsName);
 }
コード例 #4
0
        public DataFactory(string nameOrConnectionString)
        {
            _tearDownStack = new Stack <Action>();

            string connectionString = ConfigurationProvider.GetConnectionString(nameOrConnectionString) ?? nameOrConnectionString;

            _databaseDialect = DbDialectResolver.Resolve <TDialect>(connectionString);
        }
コード例 #5
0
ファイル: DBHelperImpl.cs プロジェクト: bluedusk/DBHelper
 public DBHelperImpl(string dsName)
 {
     this.m_dataSourceName = dsName;
     //EbfDataSource dataSource = EbfDataSource.GetInstanceOf(dsName);
     //this.m_dbDialect = dataSource.DbDialect;
     this.m_dbDialect = DataSourceFactory.GetInstance(dsName).DbDialect;
     this.m_dbConnection = this.CreateDbConnection(dsName);
 }
コード例 #6
0
        /// <summary>
        /// 参数字典转为数组
        /// </summary>
        /// <param name="d"></param>
        /// <param name="paramMap"></param>
        /// <returns></returns>
        public static DbParameter[] ParameterMapToArray(IDbDialect d, IDictionary <string, object> paramMap)
        {
            List <DbParameter> parameters = new List <DbParameter>();

            foreach (KeyValuePair <String, Object> p in paramMap)
            {
                parameters.Add(d.CreateParameter(p.Key, p.Value ?? ""));
            }
            return(parameters.ToArray());
        }
コード例 #7
0
        /// <summary>
        /// Creates a new database command.
        /// </summary>
        /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
        /// <param name="commandText">The SQL statement associated with this <see cref="IDbCommand"/>.</param>
        public static IDbCommand CreateCommand(this IDbDialect dialect, String commandText)
        {
            var command = dialect.Provider.CreateCommand();

            Debug.Assert(command != null);

            command.CommandText = commandText;

            return(command);
        }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of <see cref="DbBatchOperation"/>.
 /// </summary>
 /// <param name="commandTemplate">The <see cref="DbCommand"/> template on which the <see cref="DbBatchOperation"/> is based.</param>
 /// <param name="dialect">The <see cref="IDbDialect"/> associated with this <see cref="DbBatchOperation"/>.</param>
 /// <param name="batchSize">The maximum number of items to be written in a batch.</param>
 /// <param name="flushInterval">The frequency with which the current batch is to be flushed.</param>
 public DbBatchOperation(IDbDialect dialect, IDbCommand commandTemplate, Int32 batchSize, TimeSpan flushInterval)
 {
     this.autoFlush = true;
     this.dialect = dialect;
     this.batchSize = batchSize;
     this.flushInterval = flushInterval;
     this.commandTemplate = commandTemplate.Clone();
     this.waitHandle = new ManualResetEvent(initialState: false);
     this.backgroundWorker = new Thread(WaitForData) { IsBackground = true };
     this.buffer = CreateBuffer(commandTemplate);
     this.backgroundWorker.Start();
 }
コード例 #9
0
        private TableGenerator(IDbDialect dbDialect, string tableName, Stack <RecordIdentifier> generatedRecords, int depth)
        {
            _dbDialect        = dbDialect;
            _tableName        = tableName;
            _generatedRecords = generatedRecords;
            _depth            = depth;

            // Try get the column info for this table in memory cache, else, get from dialect and store on cache
            _tableColumnInfoList = MemoryCache.TryGetFromCache <IEnumerable <TableColumnInfo> >(tableName, () =>
            {
                return(_dbDialect.GetTableSchemaInfo(tableName));
            });
        }
コード例 #10
0
ファイル: BaseDAL.cs プロジェクト: yushuo1990/cms
 private static void CheckAndInit()
 {
     if (!_inited)
     {
         DataBaseAccess _db = CmsDataBase.Instance;
         if (_db == null)
         {
             throw new ArgumentNullException("_db");
         }
         DbFact = _db.GetDialect();
         //SQLPack对象
         _inited = true;
     }
 }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of <see cref="DbBatchOperation"/>.
 /// </summary>
 /// <param name="commandTemplate">The <see cref="DbCommand"/> template on which the <see cref="DbBatchOperation"/> is based.</param>
 /// <param name="dialect">The <see cref="IDbDialect"/> associated with this <see cref="DbBatchOperation"/>.</param>
 /// <param name="batchSize">The maximum number of items to be written in a batch.</param>
 /// <param name="flushInterval">The frequency with which the current batch is to be flushed.</param>
 public DbBatchOperation(IDbDialect dialect, IDbCommand commandTemplate, Int32 batchSize, TimeSpan flushInterval)
 {
     this.autoFlush        = true;
     this.dialect          = dialect;
     this.batchSize        = batchSize;
     this.flushInterval    = flushInterval;
     this.commandTemplate  = commandTemplate.Clone();
     this.waitHandle       = new ManualResetEvent(initialState: false);
     this.backgroundWorker = new Thread(WaitForData)
     {
         IsBackground = true
     };
     this.buffer = CreateBuffer(commandTemplate);
     this.backgroundWorker.Start();
 }
コード例 #12
0
ファイル: SqlQuery.cs プロジェクト: 921819535/cms
 /// <summary>
 /// 转换参数
 /// </summary>
 /// <param name="db"></param>
 /// <returns></returns>
 public bool Parse(IDbDialect db)
 {
     if (this.parameters == null)
     {
         if (this.dataArray != null)
         {
             this.parameters = DataUtil.ToParams(db, this.dataArray);
             return(true);
         }
         if (this.dataMap != null)
         {
             this.parameters = DataUtil.ParameterMapToArray(db, this.dataMap);
             return(true);
         }
     }
     return(false);
 }
コード例 #13
0
        /// <summary>
        /// Opens a new database connection.
        /// </summary>
        /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
        public static IDbConnection OpenConnection(this IDbDialect dialect)
        {
            var connection = dialect.Provider.CreateConnection();

            Debug.Assert(connection != null);

            try
            {
                connection.ConnectionString = dialect.ConnectionString;
                connection.Open();
            }
            catch (Exception)
            {
                connection.Dispose();
                throw;
            }

            return(connection);
        }
コード例 #14
0
        /// <summary>
        /// 转换为参数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DbParameter[] ToParams(IDbDialect db, object[,] data)
        {
            if (data != null)
            {
                int l = data.GetLength(0);
                if (l != 0 && data.GetLength(1) != 2)
                {
                    throw new ArgumentOutOfRangeException("data", "多纬数组的二维长度必须为2");
                }

                DbParameter[] parameter = new DbParameter[l];
                for (int i = 0; i < l; i++)
                {
                    parameter[i] = db.CreateParameter(data[i, 0].ToString(), data[i, 1]);
                }
                return(parameter);
            }
            return(null);
        }
コード例 #15
0
        /// <summary>
        /// Executes the specified command.
        /// </summary>
        /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
        /// <param name="command">The command to execute.</param>
        /// <param name="executor">The executor used to execute the command.</param>
        private static TResult ExecuteCommand <TResult>(this IDbDialect dialect, IDbCommand command, Func <TResult> executor)
        {
            TResult result;

            try
            {
                using (var connection = dialect.OpenConnection())
                    using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                    {
                        command.Connection  = connection;
                        command.Transaction = transaction;

                        result = executor();

                        transaction.Commit();
                    }
            }
            catch (DbException ex)
            {
                throw dialect.Translate(command, ex);
            }

            return(result);
        }
コード例 #16
0
ファイル: DataSource.cs プロジェクト: bluedusk/DBHelper
        private DataSourceFactory(string name)
        {
            name = name.ToLower();
            DataSourceConfig dsConfig = DBHelperFactory2.DataSourceMap[name];
            this.m_dataSourceName = dsConfig.dataSourceName;
            this.m_connectionString = dsConfig.connStr;
            this.m_providerName = dsConfig.providerName;
            string dbDialectClass = dsConfig.dialectClass;
            m_dbDialect = (IDbDialect)ToolKit.CreateInstance(dbDialectClass);

            //默认配置
            this.m_minSize = 50;
            this.m_maxSize = 100;
            this.m_usePool = true;
            this.m_timeOut = 1000;
            try { this.m_minSize = dsConfig.minSize; }
            catch { }
            try { this.m_maxSize = dsConfig.maxSize; }
            catch { }
            try { this.m_timeOut = dsConfig.timeOut; }
            catch { }
            try { this.m_usePool = dsConfig.pooled; }
            catch { }
        }
コード例 #17
0
        public static DbContextOptionsBuilder UseDialect(this DbContextOptionsBuilder builder, IDbDialect dialect)
        {
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }

            builder.Options.Dialect = dialect;

            return(builder);
        }
コード例 #18
0
 public TableGenerator(IDbDialect dbDialect, string tableName)
     : this(dbDialect, tableName, new Stack <RecordIdentifier>(), 0)
 {
 }
コード例 #19
0
 /// <summary>
 /// Executes a SQL statement against a connection object.
 /// </summary>
 /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
 /// <param name="command">The command to execute.</param>
 public static Int32 ExecuteNonQuery(this IDbDialect dialect, IDbCommand command)
 {
     return(dialect.ExecuteCommand(command, command.ExecuteNonQuery));
 }
コード例 #20
0
 /// <summary>
 /// Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.
 /// </summary>
 /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
 /// <param name="command">The command to execute.</param>
 public static Object ExecuteScalar(this IDbDialect dialect, IDbCommand command)
 {
     return(dialect.ExecuteCommand(command, command.ExecuteScalar));
 }
コード例 #21
0
        public DataFactory(IDbDialect databaseDialect)
        {
            _tearDownStack = new Stack <Action>();

            _databaseDialect = databaseDialect;
        }
コード例 #22
0
 /// <summary>
 /// Queries the databse for a single database record returning default <typeparamref name="T"/> if not found.
 /// </summary>
 /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
 /// <param name="command">The command to execute.</param>
 /// <param name="recordBuilder">The record builder that maps an <see cref="IDataRecord"/> to <typeparamref name="T"/>.</param>
 public static T QuerySingle <T>(this IDbDialect dialect, IDbCommand command, Func <IDataRecord, T> recordBuilder)
 {
     return(dialect.ExecuteCommand(command, () => ExecuteReader(command, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow, recordBuilder).SingleOrDefault()));
 }
コード例 #23
0
 /// <summary>
 /// Queries the databse for a zero or more database records.
 /// </summary>
 /// <param name="dialect">The <see cref="IDbDialect"/>.</param>
 /// <param name="command">The command to execute.</param>
 /// <param name="recordBuilder">The record builder that maps an <see cref="IDataRecord"/> to <typeparamref name="T"/>.</param>
 public static List <T> QueryMultiple <T>(this IDbDialect dialect, IDbCommand command, Func <IDataRecord, T> recordBuilder)
 {
     return(dialect.ExecuteCommand(command, () => ExecuteReader(command, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, recordBuilder)));
 }