如何将 sqlDataReader 转换为任务异步
How to convert sqlDataReader to Task async
我有以下代码 - 谁能告诉我是否可以将其设为异步任务
public MDTO GetIe(MDTO dtoItem)
{
string[] Tables = new string[] { "C", "CValue", "SP", "Ar", "P", "CR", "QC", "SR" };
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["tESTp"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("GetItem", connection))
{
DataSet Result = new DataSet();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ProjectId", SqlDbType.VarChar);
command.Parameters["@ProjectId"].Value = dtoItem.ProjectId;
connection.Open();
Result.EnforceConstraints = false;
Result.Load(command.ExecuteReader(CommandBehavior.CloseConnection), LoadOption.OverwriteChanges, Tables);
dtoItem.SR = Result;
}
}
return dtoItem;
}
将您的 command.ExecuteReader 更改为异步版本:
public async Task<MDTO> GetIeAsync(MDTO dtoItem)
{
...
connection.Open();
Result.EnforceConstraints = false;
SqlDataReader dataReader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
Result.Load(dataReader, LoadOption.OverwriteChanges, Tables);
dtoItem.SR = Result;
...
}
我有以下代码 - 谁能告诉我是否可以将其设为异步任务
public MDTO GetIe(MDTO dtoItem)
{
string[] Tables = new string[] { "C", "CValue", "SP", "Ar", "P", "CR", "QC", "SR" };
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["tESTp"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("GetItem", connection))
{
DataSet Result = new DataSet();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ProjectId", SqlDbType.VarChar);
command.Parameters["@ProjectId"].Value = dtoItem.ProjectId;
connection.Open();
Result.EnforceConstraints = false;
Result.Load(command.ExecuteReader(CommandBehavior.CloseConnection), LoadOption.OverwriteChanges, Tables);
dtoItem.SR = Result;
}
}
return dtoItem;
}
将您的 command.ExecuteReader 更改为异步版本:
public async Task<MDTO> GetIeAsync(MDTO dtoItem)
{
...
connection.Open();
Result.EnforceConstraints = false;
SqlDataReader dataReader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
Result.Load(dataReader, LoadOption.OverwriteChanges, Tables);
dtoItem.SR = Result;
...
}