函数回调方式

 用代码说明

在页面上放Gridview用于显示

<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();
        }

    }
}


 

 

 

相关文章:

  • 2022-12-23
  • 2021-10-30
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2022-03-04
猜你喜欢
  • 2022-12-23
  • 2021-08-18
  • 2021-07-06
  • 2021-07-05
  • 2021-07-30
  • 2022-12-23
  • 2021-07-18
相关资源
相似解决方案