DataSet
最近在AJAX 开发中, 需要调用业务函数,操作Dataset, 读取数据集,具体操作方法如下: 新建一 WEB 项目,创建一业务类:如下所示:
using System; using System.Data; using System.Configuration; using System.Web; 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.Data.OleDb; using System.Text;
/**//// <summary> /// test 的摘要说明 /// </summary> public class test { public test() { // // TODO: 在此处添加构造函数逻辑 // }
// 数据集传递测试 [Ajax.AjaxMethod()] public DataSet GetDataSet() { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:工作项目分析 estdb.mdb;Persist Security Info=True;"); DataSet ds = new DataSet(); try { OleDbCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from t_name"; cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(ds); return ds; } catch { conn.Close(); throw; } } } 创建好业务类以后,再新建一 Aspx 页面,在页面的 Page_Load 中注册业务类: protected void Page_Load(object sender, EventArgs e) { Ajax.Utility.RegisterTypeForAjax(typeof(test)); } 页面代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="read_dataset.aspx.cs" Inherits="read_dataset" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>read_dataset</title>
<script language="JavaScript">
function getDataSet() { var ds =test.GetDataSet().value; if(ds != null && typeof(ds) == "object" && ds.Tables != null) { var s = new Array(); s[s.length] = "<table style='border: #000000 1px solid; color: #993333; font-family: 'Microsoft Sans Serif'; background-color: #ffff99;'>";
for(var i=0; i<ds.Tables[0].Rows.length; i++) { s[s.length] = "<tr>"; s[s.length] = "<td>" + ds.Tables[0].Rows[i].id + "</td>"; s[s.length] = "<td>" + ds.Tables[0].Rows[i].f_date + "</td>"; s[s.length] = "<td>" + ds.Tables[0].Rows[i].f_name + "</td>"; s[s.length] = "</tr>"; }
s[s.length] = "</table>"; document.getElementById("div1").innerHTML = s.join(""); } else { alert("调用Ajax接口函数错误!"); } } </script>
</head> <body> <form id="form1" runat="server"> <input type="button" value="dataset" onclick="getDataSet();" /> <div id="div1"> </div> </form> </body> </html>
执行后,可以读取数据集的数。
|