using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;using System.Web.Services;using System.Threading;public partial class Default2 : System.Web.UI.Page{ string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnection"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } private void BindData() { string Sql = "SELECT TOP 10 * FROM customers"; using (SqlConnection Con = new SqlConnection(ConStr)) { using (SqlCommand Cmd = new SqlCommand(Sql, Con)) { Con.Open(); DataTable dt = new DataTable(); using (SqlDataReader Dr = Cmd.ExecuteReader()) { dt.Load(Dr); gvNorth.DataSource = dt; gvNorth.DataBind(); } } } } [WebMethod] public static string GetData() { Thread.Sleep(10000); //return "this is tommy test"; string ConStrs = ConfigurationManager.ConnectionStrings["NorthwindConnection"].ConnectionString; string Sql = "SELECT TOP 3 CustomerID,CompanyName,ContactName FROM customers "; string Result = string.Empty; using (SqlConnection Con = new SqlConnection(ConStrs)) { using (SqlCommand Cmd = new SqlCommand(Sql, Con)) { Con.Open(); DataTable dt = new DataTable(); using (SqlDataReader Dr = Cmd.ExecuteReader()) { while (Dr.Read()) { Result += Dr[0].ToString() + ";"; } } } } return Result; } // 供前台AJAX调用 [WebMethod] public static DataTable GetDataTable() { Thread.Sleep(10000); string ConStrs = ConfigurationManager.ConnectionStrings["NorthwindConnection"].ConnectionString; string Sql = "SELECT TOP 3 CustomerID,CompanyName,ContactName FROM customers"; DataTable ResultTable = new DataTable(); DataTable OrderTable = new DataTable(); using (SqlConnection Con = new SqlConnection(ConStrs)) { using (SqlCommand Cmd = new SqlCommand(Sql, Con)) { Con.Open(); DataTable dt = new DataTable(); using (SqlDataReader Dr = Cmd.ExecuteReader()) { ResultTable.Load(Dr); } } } return ResultTable; }}