关于 SqlDataReader 并发问题

版权声明:好日子www.34314.com https://blog.csdn.net/www34314com/article/details/89520461

环境如下:

部署了一个 WebAPI 项目。

1秒几十条 或是 按着 F5 不放(几十秒) 通过ADO.NET 访问数据库结构会 链接会挂掉。

有谁有相关经验可以解决此问题

ADO.NET 部分代码如下:

C# code

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

///
/// 获取映射后类型为 T 的数据实体。
///
/// 映射的数据实体对象类型。
/// 针对数据源运行的文本命令。
/// 指示或指定如何解释 System.Data.IDbCommand.CommandText 属性。
/// 针对数据源运行的文本命令的参数集合。
/// 执行数据读取器或数据映射到实体出错。
/// 映射后类型为 T 的数据实体。
public T GetMappedDataEntity(StringBuilder commandText, CommandType commandType, params IDataParameter[] dataParameters)
where T : INamingContainerEntity, new()
{
T t = default(T);
IDataReader dataReader = null;
try
{
IDictionary<string, PropertyInfo> properties = ExtractMappingDataAttribute<T, MappingDataFieldAttribute>.GetProperties();
if (properties != null && properties.Count > 0)
{
dataReader = this.ExecuteReader(commandType, commandText, dataParameters);
if (dataReader.Read())
{
t = SetPropertyValue(dataReader, properties);
}
dataReader.Close();
this.MainConnection.Close();
}
}
catch (DbHelperException ex)
{
throw new DbConverterException(ex.Message, ex);
}
finally
{
if (dataReader != null && !dataReader.IsClosed)
dataReader.Close();
this.MainConnection.Close();
}
return t;
}

猜你喜欢

转载自blog.csdn.net/www34314com/article/details/89520461