如何取消 SqlDataAdapter.Fill() 的执行(转载)
问
Scenario: We have a DataGridView which is attached to DataAdapter (datatable), we load the data in datatable using (adapter.fill(query, datatable)) in a separate thread (using delegate and beginInvoke) and once the data is loaded we attached that datatable to datagridview (in the main thread)
Is there a way we can check if fill() is still executing and cancel it.
Real scenario: User click on the user name and corresponding data is loaded in the datagrid. Sometime, user is impatient and click on the another user (here I want to cancel the previous fill and start a new fill)
UPDATE: We keep two DataApdaters (and two DataTables) and we attach one datatable to datagridview and start loading data to another datatable asynchronously. When data is loaded we simply bind the datagridview to DataTable which we just filled (and start loading the previous datable asynchronously) This way UI will always get the current data (without user waiting on UI to refresh or hang)
答
You can provide a SqlCommand to adapter constructor and invoke a Cancel method on it. There is a raw template :
class Model
{
private SqlCommand loadUserCommand;
private DataTable userData; public void LoadUser(string userId)
{
loadUserCommand = GetUserLoadCommandForUserID(userId);
userData = new DataTable("userData");
using (var adapter = new SqlDataAdapter(loadUserCommand))
{
adapter.Fill(userData);
}
} public void AbortLoadUser()
{
if (loadUserCommand!= null)
loadUserCommand.Cancel();
} private SqlCommand GetUserLoadCommandForUserID(string userId)
{
var connection = new SqlConnection("...");
var command = connection.CreateCommand();
...
}
}
注意,在执行SqlCommand.Cancel()方法时,如果SqlDataAdapter.Fill方法还没有执行完毕,那么SqlDataAdapter.Fill方法会抛出SqlException异常,所以为了安全起见,我们应该在代码中包含异常捕获和处理逻辑,如下所示:
class Model
{
private SqlCommand loadUserCommand;
private DataTable userData; public void LoadUser(string userId)
{
loadUserCommand = GetUserLoadCommandForUserID(userId);
userData = new DataTable("userData"); try
{
using (var adapter = new SqlDataAdapter(loadUserCommand))
{
adapter.Fill(userData);
}
}
catch (SqlException ex)
{
//异常处理逻辑
}
} public void AbortLoadUser()
{
if (loadUserCommand != null)
loadUserCommand.Cancel();
} private SqlCommand GetUserLoadCommandForUserID(string userId)
{
var connection = new SqlConnection("...");
var command = connection.CreateCommand();
...
}
}
同理,SqlCommand.Cancel()方法也可以用来取消SqlDataReader的执行,如下所示:
using Microsoft.Data.SqlClient;
using System;
using System.Data;
using System.Threading.Tasks; namespace NetCoreADOTesting
{
class Program
{
static SqlCommand currentCommand = null; public static async Task RunSql()
{
int row = ; try
{
string strConn = "Data Source=192.168.1.1;Initial Catalog=TestDB; User Id=sa;Password=TUI123456";
string sql = @"SELECT [ID]
,[Name]
,[Age]
FROM [dbo].[People];
waitfor delay '1:00';
"; using (SqlConnection sqlConnection = new SqlConnection(strConn))
{
sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
{
currentCommand = sqlCommand;
sqlCommand.CommandTimeout = ; using (SqlDataReader sqlDataReader = currentCommand.ExecuteReader())
{
if (sqlDataReader.HasRows)
{
while (sqlDataReader.Read())
{
//有数据从SqlDataReader中读到
row++;
}
}
}
}
} Console.WriteLine("SqlDataReader row count is {0}", row);
}
catch (SqlException ex)
{
//异常处理逻辑
Console.WriteLine(ex.ToString());
} await Task.CompletedTask;
} static void Main(string[] args)
{
Task t = Task.Run(async () =>
{
await RunSql();
}); Console.WriteLine("Press any key to cancel...");
Console.ReadKey(); if (currentCommand != null)
{
currentCommand.Cancel();
} Console.WriteLine("Press any key to end...");
Console.ReadKey();
}
}
}
这里有一点需要注意,如果SqlCommand.Cancel()方法在生成SqlDataReader之前就被调用了(上面黄色代码行之前),是不会起作用的, SqlDataReader还是会被执行,除非再次调用SqlCommand.Cancel()方法,SqlDataReader的执行才会被取消,并抛出SqlException异常。也就是说SqlCommand.Cancel()方法一定要在SqlDataReader执行之后调用,才能取消SqlDataReader的执行。同样,SqlCommand.Cancel()方法如果在SqlDataAdapter.Fill方法之前执行,也是不起作用的,这是SqlCommand.Cancel()方法的一个缺陷,因为调用SqlCommand.Cancel()方法时,我们并不知道SqlDataReader和SqlDataAdapter是否正在执行。
因此最好的办法还是用SqlCommand.ExecuteReaderAsync(CancellationToken cancellationToken)方法和SqlDataReader.ReadAsync(CancellationToken cancellationToken)方法,结合CancellationToken参数来取消执行。注意,如果CancellationToken参数被取消,ExecuteReaderAsync和ReadAsync方法会抛出TaskCanceledException异常,同样我们在代码中要包含异常捕获和处理逻辑,如下所示:
using Microsoft.Data.SqlClient;
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks; namespace NetCoreADOTesting
{
class Program
{
static CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); public static async Task RunSql()
{
int row = ; try
{
string strConn = "Data Source=192.168.1.1;Initial Catalog=TestDB; User Id=sa;Password=TUI123456";
string sql = @"SELECT [ID]
,[Name]
,[Age]
FROM [dbo].[People];
waitfor delay '1:00';
"; using (SqlConnection sqlConnection = new SqlConnection(strConn))
{
sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
{
sqlCommand.CommandTimeout = ; using (SqlDataReader sqlDataReader = await sqlCommand.ExecuteReaderAsync(cancellationTokenSource.Token))
{
if (sqlDataReader.HasRows)
{
while (await sqlDataReader.ReadAsync(cancellationTokenSource.Token))
{
//有数据从SqlDataReader中读到
row++;
}
}
}
}
} Console.WriteLine("SqlDataReader row count is {0}", row);
}
catch (TaskCanceledException ex)
{
//异常处理逻辑
Console.WriteLine(ex.ToString());
}
} static void Main(string[] args)
{
Task t = Task.Run(async () =>
{
await RunSql();
}); Console.WriteLine("Press any key to cancel...");
Console.ReadKey(); if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
} Console.WriteLine("Press any key to end...");
Console.ReadKey();
}
}
}
这样,只要我们执行了CancellationTokenSource.Cancel()方法,那么SqlCommand.ExecuteReaderAsync(CancellationToken cancellationToken)方法和SqlDataReader.ReadAsync(CancellationToken cancellationToken)方法都会被取消执行,并抛出TaskCanceledException异常。
如何取消 SqlDataAdapter.Fill() 的执行(转载)的更多相关文章
- C#中SqlDataAdapter的使用小结---转载
C#中SqlDataAdapter的使用小结 转载 叁木-Neil 最后发布于2018-06-07 21:29:39 阅读数 8275 收藏 展开 SqlDataAdapter对象 一.特点介绍1.表 ...
- C#里sqlDataAdapter.fill(DataSet,String)的用法
第二个参数 String是指定DataSet 里表的名字,例如 sqlDataAdapter.fill(DataSet,"学生表") 指定后,以后就可以这样调用这张表 DataSe ...
- iis7如何取消目录的可执行权限
我们需要把IIs中某一个目录的可执行权限去掉.这在IIs6中是非常方便的,可是因为iis7的机制小编也找了不少资料才找到. 第一步:先选择需要取消权限的目录,然后在右边可以看到 “处理程序映射” 双击 ...
- Mac OSX取消Apache(httpd)开机启动(转载)
启动服务时提示Apache启动失败,80端口被占用.查看进程发现存在几个httpd. OS X自带Apache,可是默认是没有启动的.我也没有开启Web共享,怎么就开机启动了呢? 不知道是不是因为安装 ...
- 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
T3先执行,在T3的run中,调用t2.join,让t2执行完成后再执行t3 在T2的run中,调用t1.join,让t1执行完成后再让T2执行 public class Test { // 1.现在 ...
- 神奇的问题记录【SqlDataAdapter Fill DataSet】
今天发现程序中有一张报表查询速度很慢[全条件要二分钟左右],查找相关原因,准备进行优化处理.注:报表调用存储过程,存储过程返回两个table就有以下神奇的故事: 直接将SQL语句在SSMS中执行发现全 ...
- flask请求异步执行(转载)
Flask默认是不支持非阻塞IO的,表现为: 当 请求1未完成之前,请求2是需要等待处理状态,效率非常低. 在flask中非阻塞实现可以由2种: 启用flask多线程机制 # Flask from f ...
- window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马
网站安全中,对目录的执行权限是非常敏感的,一般来说,可以写入的目录是不能够拥有脚本的执行权限的,可写入的目录如: data.uploads,data目录主要是基本配置文件和缓存数据,uploads则是 ...
- 调用SqlCommand或SqlDataAdapter的Dispose方法,是否会关闭绑定的SqlConnection?(转载)
1. Does SqlCommand.Dispose close the connection? 问 Can I use this approach efficiently? using(SqlCom ...
随机推荐
- WPF属性绑定实现双向变化
WPF依赖项属性可以实现属性的绑定,成功绑定之后只要修改后台绑定的属性,即可UI同步自动更新绑定的值,无需手动刷新界面:同样,前台的值变化后,通过获取绑定的属性值也可获取UI变化后的值,实现双向变化的 ...
- Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
本文梯子 本文3.0版本文章 代码已上传Github+Gitee,文末有地址 大神反馈: 零.今天完成的深红色部分 一.AOP 之 实现日志记录(服务层) 1.定义服务接口与实现类 2.在API层中添 ...
- [转]The Regular Expression Object Model
本文转自:https://docs.microsoft.com/en-us/dotnet/standard/base-types/the-regular-expression-object-model ...
- android ANR 分析定位问题
ANR ? android 规定,Activity如果5秒钟之内无法响应屏幕触摸事件或者键盘输入事件,BroadcastReceiver 如果10s中之内还未执行完操作就会出现ANR 定位ANR问题 ...
- iOS UIPopoverView的使用
UIViewController *contentViewController = [[UIViewController alloc] init]; contentViewController.vie ...
- laravel 之路由和MVC
一.路由 Routes\; 1. 路由简介 简单的说就是将用户的请求转发给相应的程序进行处理. 作用就是建立url和程序之间的映射 请求类型get . post.put.patch.delete 2. ...
- python测试mysql写入性能完整实例
这篇文章主要介绍了python测试mysql写入性能完整实例,具有一定借鉴价值,需要的朋友可以参考下 本文主要研究的是python测试mysql写入性能,分享了一则完整代码,具体介绍如下. 测试环境: ...
- Linux—网络防火墙详解
一.防火墙基本知识 二.firewall防火墙 # 安装firewalld [root@localhost ~]# apt-get install firewalld [root@localhost ...
- mysql connector c++ 1.1 API初步体验
mysql connector c++ 1.1 API初步体验 1,常用的头文件 #include <mysql_connection.h> #include <mysql_driv ...
- [PHP] 持续交付Jenkins安装
1.下载并运行 Jenkins下载 Jenkins.http://mirrors.jenkins.io/war-stable/latest/jenkins.war 打开终端进入到下载目录.运行命令 j ...