How to use BeginExecuteReader

Good afternoon.

Please help me on how to use the three BeginExecuteReader () methods of the SqlCommand class using Generic Lists. I made a method using BeginExecuteReader, but I don't know if this is the best way to use

public class Empresa { public Empresa() { PkEmpresa = -1; CodigoEmpresa = ""; Descripcion = ""; PkCategoriaEmpresa = -1; } public int PkEmpresa { get; set; } public string CodigoEmpresa { get; set; } public string Descripcion { get; set; } public int PkCategoriaEmpresa { get; set; } public Empresa ShallowCopy() { return (Empresa)this.MemberwiseClone(); } } public class AsyncronousDAL { private static string getConexion() { return "Data Source=DATABASE;Initial Catalog=DATA_BASE;Integrated Security=True;Asynchronous Processing=True"; } public static List<Empresa> ConsultaAsincrona() { List<Empresa> _resultados = new List<Empresa>(); using (SqlConnection conexion = new SqlConnection(getConexion())) { using (SqlCommand commando = new SqlCommand("[dbo].[pruebaAsync]", conexion)) { commando.CommandType = System.Data.CommandType.StoredProcedure; conexion.Open(); IAsyncResult resultado = commando.BeginExecuteReader(); using (SqlDataReader reader = commando.EndExecuteReader(resultado)) { while (reader.Read()) { _resultados.Add(new Empresa() { PkEmpresa = Convert.ToInt32(reader["PkEmpresa"]), CodigoEmpresa = reader["CodigoEmpresa"].ToString(), Descripcion = reader["Descripcion"].ToString(), PkCategoriaEmpresa = Convert.ToInt32(reader["PkCategoriaEmpresa"]) }); } } } } return _resultados; } } 
+6
source share
2 answers

If you are new to Asynch, there are many tutorials and examples on the Internet. This is old but relevant: http://msdn.microsoft.com/en-us/library/aa719595(v=vs.71).aspx

When you call BeginExecuteReader , the work will eventually be pushed into the workflow, which will allow your main one to continue. When you call EndExecuteReader , which will block the main thread until this task is completed.

If you immediately called EndExecuteReader - you really do not get any benefit (in fact, you introduce additional overhead).

Take a look at an example here: http://msdn.microsoft.com/en-us/library/7szdt0kc.aspx

The BeginExecuteReader method returns immediately, but before the code makes a corresponding call to the EndExecuteReader method, it should not make any other calls that start synchronous or asynchronous execution of the same SqlCommand object. Calling EndExecuteReader until the command completes; the SqlCommand object is blocked until the execution completes.

This is the relevant section of code:

  // Although it is not required that you pass the // SqlCommand object as the second parameter in the // BeginExecuteReader call, doing so makes it easier // to call EndExecuteReader in the callback procedure. AsyncCallback callback = new AsyncCallback(HandleCallback); command.BeginExecuteReader(callback, command); 
+9
source

If you are going to block immediately (calling EndExecuteReader ), you should simply use ExecuteReader instead of BeginExecuteReader .

+1
source

Source: https://habr.com/ru/post/917649/


All Articles