SqlCommand怎么取消大数据量查询

来源:互联网 发布:林珊珊淘宝店 编辑:程序博客网 时间:2024/04/28 15:40

1、用异步方式进行查询

SqlConnection con;

            SqlCommand com;
            try
            {


                con = new SqlConnection("UID=sa;Password=123;Initial Catalog=AD;Data Source=192.168.1.1;Asynchronous Processing=true");
                con.Open();
                com = new SqlCommand("select top 100 * from tb_user", con);
                com.BeginExecuteReader(new AsyncCallback(
                        delegate(IAsyncResult ar)
                        {
                            if (ar.IsCompleted)
                            {
                                SqlCommand tempCom = (SqlCommand)ar.AsyncState;
                                SqlDataReader tempDr = tempCom.EndExecuteReader(ar);
                                DataTable dt = new DataTable();
                                dt.Load(tempDr);
                                tempDr.Close();
                            }
                        }
                    ), com);
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序发生错误,信息: " + ex.Message);

            }


          2、在合适的地方调用 com.Cancel();

原创粉丝点击