获取网站服务器数据库,利用XmlHttp获取服务器数据库数据以表格的方式返回客户的代码示例...

以下示例,不使用任何的无刷新组件,直接通过XmlHttp完成!

一、JS文件代码

// JScript 文件

二、ASHX文件代码

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.IO;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Security; //加密

using System.Text;

using System.Data.SqlClient;

using System.Web.SessionState;

public class GetSuggestion : IHttpHandler,IRequiresSessionState

{

public void ProcessRequest (HttpContext context)

{

context.Response.ContentType = "text/plain";

//context.Response.Write("Hello World");

DataTable dt = GetSqlDataTable();

string response = string.Empty;

for (int i = 0; i < dt.Rows.Count; i++)

{

response += dt.Rows[i][0].ToString() + "," + dt.Rows[i][1].ToString() + "," + dt.Rows[i][2].ToString() + "," + dt.Rows[i][3].ToString() + ";";

}

if (response.EndsWith(";"))

{

response = response.Substring(0, response.Length - 1);

}

context.Response.Write(response);

context.Response.End();

}

private DataTable GetSqlDataTable()

{

DataClass.SqlDataClas MyClass = new DataClass.SqlDataClas();

SqlConnection conn = new SqlConnection(MyClass.GetMyDataBaseConStr());

string query = "select 字段1,字段2,字段3,字段4 from 表名 where YouID=@MyID order by ID desc";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.Parameters.Add(new SqlParameter("@MyID", SqlDbType.VarChar, 8000));

cmd.Parameters["@MyID"].Value = HttpContext.Current.Session["变量名称"].ToString().Trim();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

try

{

conn.Open();

adapter.Fill(dt);

return dt;

}

catch

{

throw;

}

finally

{

cmd.Dispose();

conn.Close();

}

}

public bool IsReusable {

get

{

return false;

}

}

}

三、ASPX或HTML页面

里面仅需要一个按钮和一个div即可!

“”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值