函数回调方式
用代码说明
在页面上放Gridview用于显示
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvData" runat="server">
</asp:GridView>
</div>
</form>
</body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvData" runat="server">
</asp:GridView>
</div>
</form>
</body>
使用函数回调法方式取出数据
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AdoAsyncDB
{
/// <summary>
/// 函数回调法
/// </summary>
public partial class CallBackMehtod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetCallBackData();
}
private void GetCallBackData()
{
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
IAsyncResult AsyncResult;
DBCon = new SqlConnection();
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings["NorthWindDB"].ConnectionString;
Command.CommandText = @"select top 20 companyName,contactName,city,postalCode from dbo.Customers";
Command.CommandType = CommandType.Text;
Command.Connection = DBCon;
DBCon.Open();
AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CallBackMethod), Command);
System.Threading.Thread.Sleep(1000);
DBCon.Close();
}
public void CallBackMethod(IAsyncResult IResult)
{
SqlCommand Command = (SqlCommand)IResult.AsyncState;
SqlDataReader OrdersReader = Command.EndExecuteReader(IResult);
gvData.DataSource = OrdersReader;
gvData.DataBind();
}
}
}
using System.Data.SqlClient;
using System.Configuration;
namespace AdoAsyncDB
{
/// <summary>
/// 函数回调法
/// </summary>
public partial class CallBackMehtod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetCallBackData();
}
private void GetCallBackData()
{
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
IAsyncResult AsyncResult;
DBCon = new SqlConnection();
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings["NorthWindDB"].ConnectionString;
Command.CommandText = @"select top 20 companyName,contactName,city,postalCode from dbo.Customers";
Command.CommandType = CommandType.Text;
Command.Connection = DBCon;
DBCon.Open();
AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CallBackMethod), Command);
System.Threading.Thread.Sleep(1000);
DBCon.Close();
}
public void CallBackMethod(IAsyncResult IResult)
{
SqlCommand Command = (SqlCommand)IResult.AsyncState;
SqlDataReader OrdersReader = Command.EndExecuteReader(IResult);
gvData.DataSource = OrdersReader;
gvData.DataBind();
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥