c# – 如何使用BeginExecuteReader

美好的一天.

请帮助我使用Generic Lists如何使用sqlCommand类的三个方法BeginExecuteReader().我使用BeginExecuteReader创建了一个方法,但我不知道这是否是最好的使用方法

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;
    }
}

解决方法

如果您不熟悉Asynch模式,那么Web上有很多教程和示例.这是旧的,但仍然相关: http://msdn.microsoft.com/en-us/library/aa719595(v=vs.71).aspx

当您调用BeginExecuteReader时,工作将最终被推送到工作线程,允许您的main继续执行.当您调用EndExecuteReader时,将导致主线程阻塞,直到该任务完成.

如果您立即调用EndExecuteReader – 您实际上并没有获得任何好处(事实上,您正在引入额外的开销).

看一下这里的例子:http://msdn.microsoft.com/en-us/library/7szdt0kc.aspx

The BeginExecuteReader method returns immediately,but until the code
executes the corresponding EndExecuteReader method call,it must not
execute any other calls that start a synchronous or asynchronous
execution against the same sqlCommand object. Calling the
EndExecuteReader before the command’s execution is completed cause the
sqlCommand object to block until the execution is finished.

这是代码的相关部分:

// 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);

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString(&quot;x2&quot;));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable&lt;Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include &quot;WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...