C# 如何使用BeginExecuteReader

C# 如何使用BeginExecuteReader,c#,.net,ado.net,C#,.net,Ado.net,你好 请帮助我了解如何使用SqlCommand类的三个方法BeginExecuteReader(),使用泛型列表。我使用BeginExecuteReader创建了一个方法,但我不知道这是否是最好的使用方法 public class Empresa { public Empresa() { PkEmpresa = -1; CodigoEmpresa = ""; Descripcion = ""; PkCategoriaEmpr

你好

请帮助我了解如何使用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;
    }
}
public class Empresa{
公共企业(){
PkEmpresa=-1;
CodigoEmpresa=“”;
description=“”;
pka=-1;
}
public int PkEmpresa{get;set;}
公共字符串CodigoEmpresa{get;set;}
公共字符串描述符{get;set;}
公共int-PkCategoriaEmpresa{get;set;}
公共浅水区{
返回(Empresa)this.MemberwiseClone();
}
}
公共类异步{
私有静态字符串getConexion(){
返回“数据源=数据库;初始目录=数据库;集成安全性=真;异步处理=真”;
}
公共静态列表顾问Asincrona(){
列表_resultados=新列表();
使用(SqlConnection conexion=newsqlconnection(getConexion())){
使用(SqlCommand commando=newsqlcommand(“[dbo].[pruebaAsync]”,conexion)){
commando.CommandType=System.Data.CommandType.StoredProcess;
conexion.Open();
IAsyncResult resultado=commando.BeginExecuteReader();
使用(SqlDataReader=commando.EndExecuteReader(resultado)){
while(reader.Read()){
_resultados.Add(新的Empresa(){
PkEmpresa=Convert.ToInt32(读卡器[“PkEmpresa”]),
CodigoEmpresa=读卡器[“CodigoEmpresa”]。ToString(),
Description=读卡器[“Description”]。ToString(),
PkCategoriaEmpresa=Convert.ToInt32(读卡器[“PkCategoriaEmpresa”])
});
}
}
}
}
返回_resultados;
}
}

如果要立即阻止(调用
EndExecuteReader
),则应使用而不是
BeginExecuteReader
如果您不熟悉异步模式,网上有很多教程和示例。这很古老,但仍然相关:

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

如果您立即调用EndExecuteReader,您实际上并没有得到任何好处(实际上,您引入了额外的开销)

请看下面的示例:

BeginExecuteReader方法立即返回,但直到 执行相应的EndExecuteReader方法调用,它不能 执行启动同步或异步进程的任何其他调用 对同一个SqlCommand对象执行。呼叫 命令执行完成前的EndExecuteReader会导致 SqlCommand对象将被阻止,直到执行完成

这是代码的相关部分:

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

如果你要阻塞,为什么不直接使用ExecuteReader呢?我怎样才能使它成为不阻塞?你必须完全重写你的函数来接受回调函数。如果要直接返回
列表
,则无法避免阻塞,因为这要求列表可用。如果要使函数异步,那么根据定义,列表还不可用,您好,Reed Copsey。谢谢你的回答。我的目标是使用ADO.NET的异步方法填充DataGridView,但我可以看到哪些方法没有正确使用(或者我不知道如何使用)。你能告诉我如何正确使用吗?(对不起,我的英语,我不会说英语)@Ba3你在用.net4吗?(那里要简单得多)