Example #1
0
 /// <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));
 }
Example #2
0
 /// <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()));
 }
Example #3
0
 /// <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()));
 }
Example #4
0
 /// <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));
 }
Example #5
0
 /// <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"));
 }
Example #6
0
 /// <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));
 }