Reputation: 137
I have this method that opens the connection to sqlserver, I need it to be asynchronous, so far everything is correct, but I see that it asks to wait "await"
public async static Task<DataTable> ToList(string nombreProcedimiento, List<Parameter>? parametros = null)
{
SqlConnection conexion = new(conn);
try
{
conexion.Open();
SqlCommand cmd = new(nombreProcedimiento, conexion)
{
CommandType = CommandType.StoredProcedure
};
if (parametros != null)
{
foreach (var parametro in parametros)
{
cmd.Parameters.AddWithValue(parametro.Name, parametro.Value);
}
}
DataTable tabla = new();
SqlDataAdapter da = new(cmd);
da.Fill(tabla);
return tabla;
}
catch (Exception)
{
return null!;
}
finally
{
conexion.Close();
}
}
I see that I am missing something like that
return await sqlCommand.ExecuteNonQueryAsync();
but i can't put it because i'm not executing a query but a stored procedure
CommandType = CommandType.StoredProcedure
How can I create this async method correctly?
Upvotes: 0
Views: 79
Reputation: 127
I see in your code that you've created a SqlCommand object but never executed it. Your query Type isn't important, to execute any type of query and make your method asynchronous you have to call one of these methods provided in the SqlCommand object and await it.
Upvotes: 1