- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
美好的一天。
请帮助我了解如何使用通用列表使用 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;
}
}
最佳答案
如果您不熟悉异步模式,网上有很多教程和示例。这是旧的,但仍然相关:http://msdn.microsoft.com/en-us/library/aa719595(v=vs.71).aspx
当您调用 BeginExecuteReader
时,该工作将最终被推送到工作线程,从而允许您的主线程继续执行。当您调用 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);
关于c# - 如何使用 BeginExecuteReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10952927/
美好的一天。 请帮助我了解如何使用通用列表使用 SqlCommand 类的三种方法 BeginExecuteReader()。我用 BeginExecuteReader 做了一个方法,但我不知道这是否
我有一个方法可以从我的数据库中异步获取结果: internal class CommandAndCallback { public SqlCommand Sql { get; set; }
我正在异步调用 sql 命令的 beingexecutereader 方法,用户可以取消该调用。当取消发生时,我取消了 sqlcommand 对象,这将终止 sql server 上的执行作业。 但是
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我希望这里有人可以帮助我。 我工作的公司希望我使用 MySQL 而不是 MSSQL。所以我下载了最新的驱动程序(6.1),正在移植DB层。 但是我找不到以回调作为参数的 BeginExecuteRea
我是一名优秀的程序员,十分优秀!