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. 动态类型dynamic转换为特定类型T的方案

    需求场景:有时候我们抓到一段请求数据,JSON格式的字符串数据,需要放在接口里重现问题,我们就可能会用dynamic先接受数据,然后再转换成特定数据发出请求. 方案一:直接使用特定对象T,来接受请求数 ...

  2. 采用邻接矩阵表示图的深度优先搜索遍历(与深度优先搜索遍历连通图的递归算法仅仅是DFS的遍历方式变了)

    //采用邻接矩阵表示图的深度优先搜索遍历(与深度优先搜索遍历连通图的递归算法仅仅是DFS的遍历方式变了) #include <iostream> using namespace std; ...

  3. java基础(23):字节流、字符流

    1. 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream Out ...

  4. python基础—条件语句

    一.Python基础 1.第一句python print('hello,world') Q: 后缀名可以任意? A:  导入模块时,如果不是.py后缀,会出错. 2.两种执行的方式: -python解 ...

  5. react聊天室|react+redux仿微信聊天IM实例|react仿微信界面

    一.项目概况 基于react+react-dom+react-router-dom+redux+react-redux+webpack2.0+react-photoswipe+swiper等技术混合开 ...

  6. [转]C++字符串操作函数_tcschr、_tcsrchr

    原文出处:https://blog.csdn.net/eickandy/article/details/50083169 C++标准库函数提供了字符和字符串的操作函数,并提供了其UNICODE版本,本 ...

  7. 2019年上半年收集到的人工智能LSTM干货文章

    2019年上半年收集到的人工智能LSTM干货文章 门控神经网络:LSTM 和 GRU 简要说明 LSTM-CNN-Attention算法系列之一:LSTM提取时间特征 对时间序列分类的LSTM全卷积网 ...

  8. [b0026] python 归纳 (十一)_线程_threading.Thread

    总结: 默认父线程跑完,子线程并不会马上退出,不像 thread.start_threadXXXX 父线程跑完了,并没有退出,一直在那里 线程启动速度很快,不占多少开销,不到1毫 秒 代码: # -* ...

  9. tomcat快速入门

    简介 Tomcat 是什么 Tomcat 是由 Apache 开发的一个 Servlet 容器,实现了对 Servlet 和 JSP 的支持,并提供了作为Web服务器的一些特有功能,如Tomcat管理 ...

  10. (day53)五、模型层(ORM)、settings文件配置

    目录 一.settings配置 (一)测试文件配置 (二)查看对应sql语句 二.模型表数据的增删改查 (一)创建数据 (1)create方法 (2)利用对象的绑定方法 (二)修改数据 (1)利用qu ...