如何取消 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 ...
随机推荐
- C# event
class Program { static void Main(string[] args) { Thermostat thermostat = new Thermostat(); Heater h ...
- Python打包成exe文件很难?一分钟即可学会,并添加图标!
环境1.python 3.72.pyinstaller下载方式:2.1 python安装(略)2.2 安装pyinstaller打开DOS窗口输入以下命令:pip install pyinstalle ...
- Vue中v-on的指令以及一些其他指令
1.v-on的基本使用 <div id="app"> <!-- 使用事件绑定的简写形式 --> <input type="button&qu ...
- PHP+Ajax点击加载更多列表数据实例
一款简单实用的PHP+Ajax点击加载更多列表数据实例,实现原理:通过“更多”按钮向服务端发送Ajax请求,PHP根据分页参数查询将最新的几条记录,数据以JSON形式返回,前台Query解析JSON数 ...
- python 通过使用pandas的实现的Excel的批量转换CSV文件的处理
---恢复内容开始--- 最近同事在处理文件导入的时候需要把一批文件换成CSV的格式,但是直觉修改后缀是不生效的,而且xlsx和xls的文件没法直接换成CVS的文件,所以找了一下方式,并且自己实现了p ...
- ARP攻击 winpcap
ARP攻击就是通过伪造IP地址和MAC地址实现ARP欺骗.解决办法详见百科 #define ETHER_ADDR_LEN 6 typedef struct { u_char DestMAC[ETHER ...
- tableView左划自定义带图片按钮
本方法实现的原理是将自定义按钮加在tableViewCell.contentView的屏幕外的frame上,打个比方,如果是5系的话,那么你自定义按钮的frame的起点就在(320+,0)(320+表 ...
- iOS中WebSocket的使用
https://github.com/square/SocketRocket 简单使用如下 1.初始化socket _webSocket = [[SRWebSocket alloc] initWith ...
- Vue实战狗尾草博客后台管理系统第三章
Vue实现狗尾草博客后台管理系统第三章 本章节,咱们开发管理系统侧边栏及面包屑功能. 先上一张效果图 样式呢,作者前端初审,关于设计上毫无美感可言,大家可根据自己情况设计更好看的哦~ 侧边栏 这里我们 ...
- 14.Java基础_函数/函数重载/参数传递
Java函数和函数重载 /* 函数定义: public static 返回类型 func(参数){ 方法体: } 函数重载 在调用时,Java虚拟机会通过参数的不同来区分同名的函数 满足: 1.多个函 ...