/// <summary> /// 异步取出第一条数据 /// </summary> /// <typeparam name="T">数据返回类型</typeparam> /// <param name="query">请求内容</param> /// <returns>单条数据内容</returns> public async Task <T> ExecuteScalarAsync <T>(SqlQuery query) { Open(); PrepareSqlQuery(query); return(await SqlMonitorUtil.MonitorAsync(query, async() => { return await _conn.ExecuteScalarAsync <T>(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType); }, dbType : DatabaseTyoe.ToString(), dbConnection : Connection)); }
/// <summary> /// 查询 /// </summary> /// <typeparam name="T">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <returns>结果集</returns> public IEnumerable <T> Query <T>(SqlQuery query) { Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { return _conn.Query <T>(query.CommandText, query.ParaItems, Tran, true, query.CommandTimeout, query.CommandType); }, dbType: DatabaseTyoe.ToString(), dbConnection: Connection)); }
/// <summary> /// 取出第一条数据 /// </summary> /// <typeparam name="T">数据返回类型</typeparam> /// <param name="query">请求内容</param> /// <returns>单条数据内容</returns> public T ExecuteScalar <T>(SqlQuery query) { Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { return _conn.ExecuteScalar <T>(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType); }, dbType: DatabaseTyoe.ToString(), dbConnection: Connection)); }
/// <summary> /// 异步执行增删改方法,返回受影响的行数 /// </summary> /// <param name="query">请求内容</param> /// <returns>受影响的行数</returns> public async Task <int> ExecuteNonQueryAsync(SqlQuery query) { Open(); PrepareSqlQuery(query); return(await SqlMonitorUtil.MonitorAsync(query, async() => { return await _conn.ExecuteAsync(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType); }, dbType : DatabaseTyoe.ToString())); }
/// <summary> /// 返回第一条结果信息 /// </summary> /// <typeparam name="T">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <returns>第一条结果信息</returns> public T QuerySingleOrDefault <T>(SqlQuery query) { Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { return _conn.QueryFirstOrDefault <T>(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType); }, dbType: DatabaseTyoe.ToString(), dbConnection: Connection)); }
/// <summary> /// 执行增删改方法,返回受影响的行数 /// </summary> /// <param name="query">请求内容</param> /// <returns>受影响的行数</returns> public int ExecuteNonQuery(SqlQuery query) { Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { return _conn.Execute(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType); }, dbType: DatabaseTyoe.ToString())); }
/// <summary> /// 查询 /// </summary> /// <typeparam name="T">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <returns>结果集</returns> public async Task <IEnumerable <T> > QueryAsync <T>(SqlQuery query) { Open(); PrepareSqlQuery(query); return(await SqlMonitorUtil.MonitorAsync(query, async() => { return await _conn.QueryAsync <T>(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType); }, dbType : DatabaseTyoe.ToString())); }
/// <summary> /// 异步返回多个结果 /// </summary> /// <typeparam name="TResult">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <param name="action">将GridReader转为查询结果的方法</param> /// <returns>多个结果</returns> public async Task <TResult> QueryMultipleAsync <TResult>(SqlQuery query, Func <SqlMapper.GridReader, TResult> action) { Open(); PrepareSqlQuery(query); return(await SqlMonitorUtil.MonitorAsync(query, async() => { using (var multiple = await _conn.QueryMultipleAsync(query.CommandText, param: query.ParaItems, transaction: Tran, commandTimeout: query.CommandTimeout, commandType: query.CommandType)) { return action(multiple); } }, dbType : DatabaseTyoe.ToString(), dbConnection : Connection)); }
/// <summary> /// 返回多个结果 /// </summary> /// <typeparam name="TResult">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <param name="action">将GridReader转为查询结果的方法</param> /// <returns>多个结果</returns> public TResult QueryMultiple <TResult>(SqlQuery query, Func <SqlMapper.GridReader, TResult> action) { Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { using (var multiple = _conn.QueryMultiple(query.CommandText, param: query.ParaItems, transaction: Tran, commandTimeout: query.CommandTimeout, commandType: query.CommandType)) { return action(multiple); } }, dbType: DatabaseTyoe.ToString())); }
/// <summary> /// 异步批量插入(目前只支持MSSQLServer) /// </summary> /// <param name="table">table</param> /// <param name="tableName">表名字</param> /// <returns>Task</returns> public Task BulkInsertAsync(DataTable table, string tableName) { return(SqlMonitorUtil.MonitorAsync(async() => { if (table != null && DatabaseTyoe == DatabaseType.MSSQLServer) { Open(); using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(Connection as SqlConnection, SqlBulkCopyOptions.Default, Tran as SqlTransaction)) { sqlbulkcopy.BatchSize = table.Rows.Count; sqlbulkcopy.DestinationTableName = tableName; for (int i = 0; i < table.Columns.Count; i++) { sqlbulkcopy.ColumnMappings.Add(table.Columns[i].ColumnName, table.Columns[i].ColumnName); } await sqlbulkcopy.WriteToServerAsync(table.CreateDataReader()); } } else if (table != null && DatabaseTyoe == DatabaseType.MySql) { string tmpPath = System.IO.Path.GetTempFileName(); try { string csv = table.ToCsv(); System.IO.File.WriteAllText(tmpPath, csv); Open(); MySqlBulkLoader bulk = new MySqlBulkLoader(Connection as MySqlConnection) { FieldTerminator = ",", FieldQuotationCharacter = '"', EscapeCharacter = '"', LineTerminator = "\r\n", FileName = tmpPath, NumberOfLinesToSkip = 0, TableName = tableName, }; bulk.Columns.AddRange(table.Columns.Cast <DataColumn>().Select(colum => colum.ColumnName).ToList()); int insertCount = await bulk.LoadAsync(); } finally { FileUtil.DeleteFile(tmpPath); } } else { throw new NotSupportedException(DatabaseTyoe.ToString()); } }, dbType: DatabaseTyoe.ToString(), memberName: "BaseDataAccess-BulkInsert")); }
/// <summary> /// 异步查询(Table) /// </summary> /// <param name="query">查询请求内容</param> /// <returns>Table查询结果</returns> public async Task <DataTable> QueryTableAsync(SqlQuery query) { DataTable table = new DataTable(); Open(); PrepareSqlQuery(query); return(await SqlMonitorUtil.Monitor(query, async() => { using (IDataReader reader = await _conn.ExecuteReaderAsync(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType)) { table.Load(reader); return table; } }, dbType : DatabaseTyoe.ToString(), dbConnection : Connection)); }
/// <summary> /// 查询 /// </summary> /// <typeparam name="TFirst"></typeparam> /// <typeparam name="TSecond"></typeparam> /// <typeparam name="TThird"></typeparam> /// <typeparam name="TFourth"></typeparam> /// <typeparam name="TFifth"></typeparam> /// <typeparam name="TReturn">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <param name="map">匹配关系</param> /// <param name="splitOn">分割字段</param> /// <returns>结果集</returns> public async Task <IEnumerable <TReturn> > QueryAsync <TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(SqlQuery query, Func <TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, string splitOn = "Id") { Open(); PrepareSqlQuery(query); return(await SqlMonitorUtil.MonitorAsync(query, async() => { return await _conn.QueryAsync(query.CommandText, map: map, param: query.ParaItems, transaction: Tran, splitOn: splitOn, commandTimeout: query.CommandTimeout, commandType: query.CommandType); }, dbType : DatabaseTyoe.ToString(), dbConnection : Connection)); }
/// <summary> /// 查询 /// </summary> /// <typeparam name="TFirst"></typeparam> /// <typeparam name="TSecond"></typeparam> /// <typeparam name="TReturn">返回结果类型</typeparam> /// <param name="query">查询请求内容</param> /// <param name="map">匹配关系</param> /// <param name="splitOn">The Field we should split and read the second object from (default: id)</param> /// <returns>结果集</returns> public IEnumerable <TReturn> Query <TFirst, TSecond, TReturn>(SqlQuery query, Func <TFirst, TSecond, TReturn> map, string splitOn = "Id") { Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { return _conn.Query(query.CommandText, map: map, param: query.ParaItems, transaction: Tran, splitOn: splitOn, commandTimeout: query.CommandTimeout, commandType: query.CommandType); }, dbType: DatabaseTyoe.ToString(), dbConnection: Connection)); }
/// <summary> /// 查询(Table) /// </summary> /// <param name="query">查询请求内容</param> /// <returns>Table查询结果</returns> public DataTable QueryTable(SqlQuery query) { DataTable table = new DataTable(); Open(); PrepareSqlQuery(query); return(SqlMonitorUtil.Monitor(query, () => { using (IDataReader reader = _conn.ExecuteReader(query.CommandText, query.ParaItems, Tran, query.CommandTimeout, query.CommandType)) { table.Load(reader); return table; } }, dbType: DatabaseTyoe.ToString())); }