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参数被取消,ExecuteReaderAsyncReadAsync方法会抛出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() 的执行(转载)的更多相关文章

  1. C#中SqlDataAdapter的使用小结---转载

    C#中SqlDataAdapter的使用小结 转载 叁木-Neil 最后发布于2018-06-07 21:29:39 阅读数 8275 收藏 展开 SqlDataAdapter对象 一.特点介绍1.表 ...

  2. C#里sqlDataAdapter.fill(DataSet,String)的用法

    第二个参数 String是指定DataSet 里表的名字,例如 sqlDataAdapter.fill(DataSet,"学生表") 指定后,以后就可以这样调用这张表 DataSe ...

  3. iis7如何取消目录的可执行权限

    我们需要把IIs中某一个目录的可执行权限去掉.这在IIs6中是非常方便的,可是因为iis7的机制小编也找了不少资料才找到. 第一步:先选择需要取消权限的目录,然后在右边可以看到 “处理程序映射” 双击 ...

  4. Mac OSX取消Apache(httpd)开机启动(转载)

    启动服务时提示Apache启动失败,80端口被占用.查看进程发现存在几个httpd. OS X自带Apache,可是默认是没有启动的.我也没有开启Web共享,怎么就开机启动了呢? 不知道是不是因为安装 ...

  5. 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载

    T3先执行,在T3的run中,调用t2.join,让t2执行完成后再执行t3 在T2的run中,调用t1.join,让t1执行完成后再让T2执行 public class Test { // 1.现在 ...

  6. 神奇的问题记录【SqlDataAdapter Fill DataSet】

    今天发现程序中有一张报表查询速度很慢[全条件要二分钟左右],查找相关原因,准备进行优化处理.注:报表调用存储过程,存储过程返回两个table就有以下神奇的故事: 直接将SQL语句在SSMS中执行发现全 ...

  7. flask请求异步执行(转载)

    Flask默认是不支持非阻塞IO的,表现为: 当 请求1未完成之前,请求2是需要等待处理状态,效率非常低. 在flask中非阻塞实现可以由2种: 启用flask多线程机制 # Flask from f ...

  8. window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马

    网站安全中,对目录的执行权限是非常敏感的,一般来说,可以写入的目录是不能够拥有脚本的执行权限的,可写入的目录如: data.uploads,data目录主要是基本配置文件和缓存数据,uploads则是 ...

  9. 调用SqlCommand或SqlDataAdapter的Dispose方法,是否会关闭绑定的SqlConnection?(转载)

    1. Does SqlCommand.Dispose close the connection? 问 Can I use this approach efficiently? using(SqlCom ...

随机推荐

  1. 用友的SPS定义

    基于标准产品的支持服务(Standard Product Support,SPS).主要包括:更新升级(软件补丁更新与产品升级).问题解决(产品问题在线或热线解析).知识转移(用友到客户的知识传递). ...

  2. python基础(34):线程(二)

    1. python线程 1.1 全局解释器锁GIL Python代码的执行由Python虚拟机(也叫解释器主循环)来控制.Python在设计之初就考虑到要在主循环中,同时只有一个线程在执行.虽然 Py ...

  3. 外置 tomcat 服务器设置

    外置 Tomcat 没这么太用, 今天在 windows  搭 xwiki  服务器, 比预期多花了点时间, 主要是 tomcat 环境变量没配对, tomcat 启动后闪退, 还没有日志. 最后定位 ...

  4. liteos CPU占用率(十六)

    1. 概述 1.1 基本概念 CPU(中央处理器, Central Processing Unit)占用率可以分为系统CPU占用率和任务CPU占用率两种. 系统CPU占用率(CPU Percent)是 ...

  5. 网络流(3)——找到最小st-剪切

    在大规模战争中,后勤补给是重中之重,为了尽最大可能满足前线的物资消耗,后勤部队必然要充分利用每条运输网.与此同时,交战双方也想要以最小的代价切断敌军的补给,从而使敌军处于孤立无援的境地.在古今中外的各 ...

  6. 4.Java基础_Java类型转换

    import javax.swing.plaf.synth.SynthMenuBarUI; /* 类型转换 自动类型转换: 把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量 强制类型 ...

  7. openpyxl的简单使用

    openpyxl的简单使用 openpyxl 操作excel的库,只能操作xlxs 文件, xlrd/xlwt这两个库能兼容xls(2003版) 安装 pip install openpyxl如果ex ...

  8. 高阶JS---函数柯里化

    什么是函数柯里化? 百度百科: 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的新函数的技术.通 ...

  9. 【转】Redis相关

      1. 什么是redis? Redis 是一个使用 C 语言写成的,开源的基于内存的高性能key-value数据库. Redis的值可以是由string(字符串).hash(哈希).list(列表) ...

  10. UVA10559 方块消除 Blocks(区间dp)

    一道区间dp好题,在GZY的ppt里,同时在洛谷题解里看见了Itst orz. 题目大意 有n个带有颜色的方块,没消除一段长度为 \(x\) 的连续的相同颜色的方块可以得到 \(x^2\) 的分数,用 ...