如何从ASP.NET Web表单的取消按钮停止/取消查询 [英] How to stop/cancel query from cancel button of ASP.NET web form

查看:118
本文介绍了如何从ASP.NET Web表单的取消按钮停止/取消查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单有2个按钮显示和取消。在显示按钮点击事件我显示取消按钮并执行查询,如果查询需要很长时间。我想从取消按钮取消该查询。我搜索了很多,sqlcommand .cancel()方法可能需要使用。如何使用,因为在button1写这样的



Button1_click()

{

String connectionString = WebConfigurationManager.ConnectionStrings [smitaConnectionString]。ConnectionString;

String sqlselect =从Item中选择PName;

SqlConnection con = new SqlConnection(connectionString);

尝试

{

con.Open();

SqlCommand cmd = new SqlCommand (sqlselect,con);



DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);



GridView1.DataSource = ds;

GridView1.D ataBind();



}

catch(例外情况)

{

ex.ToString();

}

终于

{

con.Close();

}

}



我尝试了什么:



Button1_click()

{

String connectionString = WebConfigurationManager.ConnectionStrings [smitaConnectionString]。ConnectionString;

String sqlselect =从Item中选择PName;

SqlConnection con = new SqlConnection(connectionString);

try

{

con.Open();

SqlCommand cmd = new SqlCommand(sqlselect,con);



DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);



GridView1.DataSource = ds;

GridView1.DataBind();



}

catch(例外情况)

{

ex.ToString(); < br $>
}

终于

{

con.Close();

}

}

I have one web form having 2 button show and cancel.In show button click event I am showing cancel button and executing query,If query take long time.I want to cancel that query from cancel button.I searched lot, sqlcommand.cancel() method may need to use.How to use that because in button1 writing like this

Button1_click()
{
String connectionString = WebConfigurationManager.ConnectionStrings["smitaConnectionString"].ConnectionString;
String sqlselect = "select PName from Item";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlselect, con);

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();

}
catch (Exception ex)
{
ex.ToString();
}
finally
{
con.Close();
}
}

What I have tried:

Button1_click()
{
String connectionString = WebConfigurationManager.ConnectionStrings["smitaConnectionString"].ConnectionString;
String sqlselect = "select PName from Item";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlselect, con);

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();

}
catch (Exception ex)
{
ex.ToString();
}
finally
{
con.Close();
}
}

推荐答案

你在网络应用程序中执行此操作,因为Web应用程序是无状态的。当您单击查询按钮时,您的请求将转到Web服务器上的某个主题并开始处理返回该请求页面所需的所有内容。



单击取消按钮生成另一个请求,该请求将转到服务器上要处理的另一个线程。这两个线程彼此无关。请求流程都不知道另一个。就第二个请求而言,没有什么可以取消的。



要获得这种功能,您必须完成重新设计应用程序以支持它。类似于管理数据请求并以可取消方式执行它们的队列。



查询请求只是将数据请求发布到此队列并返回一个ID返回客户端的数据请求数。然后,客户端可以在取消请求中回发它想要取消的请求的ID号。请求返回到服务器,该服务器返回到您的数据队列并取消相应的数据请求ID。
You const do this in a web app because web apps are stateless. When you click on the "query" button, your request goes to a thread on the webserver and starts processing everything it needs to return a page for that request.

Clicking a "cancel" button generates another request that goes to another thread on the server to process. These two threads have nothing to do with each other. Neither request process knows about the other. There is nothing to cancel as far as the second request is concerned.

To get this kind of functionality you'd have to complete redesign your application to support it. Something like a queue that manages data requests and executes them in a cancelable way.

The "query" request would just post the data request to this queue and return an ID number of the data request back to the client. The client could then, in a "cancel" request, post back the ID number of the request it wants to cancel. The request goes back to the server which goes back to your data queue and cancels the appropriate data request Id.


这篇关于如何从ASP.NET Web表单的取消按钮停止/取消查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆