作者:great_domino 的 Blog
檔案構成:
Access資料庫名為 MyData.Mdb, 裡面建了一個名為count的表,表由兩個
欄位組成: ID和COUNT,表裡有一條資料: (''''count'''',''''100'''')。
文字檔名為 count.txt,裡面隨便寫入一個數字。
靜態頁面名為Cnt.htm。
以上3個檔案都放在同一目錄下。
由於採用Access資料庫,而它採用非標準SQL文法,所以要注意在表名
和欄位名上需要加方括弧: []
下面是頁面裡的代碼:
其中getCountFromDB方法是對Access資料庫進行操作,getCountFromTxt
方法是對txt純文字進行操作,這兩個方法都在本地執行通過。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function getCountFromDB() {
//以當前分頁檔為基礎,找到檔案所在的絕對路徑。
var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm"));
var path = filePath + "MyData.mdb";
//去掉字串中最前面的"files://"這8個字元。
path = path.substring(8);
var updateCnt = 0;
//產生查詢和更新用的sql語句。
var sqlSelCnt = "SELECT COUNT FROM [COUNT] WHERE ID = ''''count''''";
var sqlUpdCnt = "UPDATE [COUNT] SET [COUNT] = ''''";
//建立串連,並產生相關字串 http://www.zhangpeng.com.cn/。
var con = new ActiveXObject("ADODB.Connection");
con.Provider = "Microsoft.Jet.OLEDB.4.0";
con.ConnectionString = "Data Source=" + path;
con.open;
var rs = new ActiveXObject("ADODB.Recordset");
rs.open(sqlSelCnt, con);
while (!rs.eof) {
var cnt = rs.Fields("COUNT");
document.write(cnt);
//將取得結果加1後更新資料庫。
updateCnt = cnt * 1 + 1;
rs.moveNext;
}
rs.close();
rs = null;
sqlUpdCnt = sqlUpdCnt + updateCnt + "''''";
con.execute(sqlUpdCnt);
con.close();
con = null;
}
function getCountFromTxt() {
var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm"));
var path = filePath + "count.txt";
path = path.substring(8);
var nextCnt = 0;
var fso, f1, ts, s;
//以唯讀方式開啟文字檔。
var ForReading = 1;
//以讀寫方式開啟文字檔。
var ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.GetFile(path);
ts = f1.OpenAsTextStream(ForReading, true);
s = ts.ReadLine();
nextCnt = eval(s) + 1;
document.write("now count is :" + s);
ts.Close();
ts = f1.OpenAsTextStream(ForWriting, true);
ts.WriteLine(nextCnt);
ts.close();
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
getCountFromTxt();
//-->
</SCRIPT>
</BODY>
</HTML>
補充:
ajaxpro使用(
http://blog.sina.com.cn/s/blog_50761d850100b3m3.html
AjaxPro 返回 DataSet/DataTable/DataView 如何呈現?
[code=JSscript]
// js
<script type="text/javascript">
function GetProductData()
{
var cb = function(res) {
if(res.error) return alert("發生錯誤\n" + res.error.Message);
//debugger;
//alert(res);
var ds = res.value;
var tbl = ds.Tables[0];
var tblHtml = " <table border=1>";
// 表頭
tblHtml += " <tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += " <th>" + tbl.Columns[j].Name + " </th>";
}
tblHtml += " </tr>";
// 資料
for(var i = 0; i < tbl.Rows.length; i++) {
tblHtml += " <tr>";
for(var j = 0; j < tbl.Columns.length; j++) {
tblHtml += " <td>" + tbl.Rows[i][tbl.Columns[j].Name] + " </td>";
}
tblHtml += " </tr>";
}
tblHtml += " </table>";
var divPro = document.getElementById("divPro");
divPro.innerHTML = tblHtml;
}
AjaxProSample.GetProductSet(cb);
}
</script>
[/code]
-
C# code
-
// .aspx.cs[AjaxPro.AjaxNamespace("AjaxProSample")]public partial class AjaxPro_ReturnDataSet : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxPro_ReturnDataSet)); } [AjaxPro.AjaxMethod] public static DataSet GetProductSet() { return CreateSampleProductSet(); } sample data#region sample data static DataSet CreateSampleProductSet() { DataSet ds = new DataSet(); ds.Tables.Add(CreateSampleProductData()); return ds; } static DataTable CreateSampleProductData() { DataTable tbl = new DataTable("Products"); tbl.Columns.Add("ProductID", typeof(int)); tbl.Columns.Add("ProductName", typeof(string)); tbl.Columns.Add("UnitPrice", typeof(decimal)); tbl.Columns.Add("CategoryID", typeof(int)); tbl.Rows.Add(1, "Chai", 18, 1); tbl.Rows.Add(2, "Chang", 19, 1); tbl.Rows.Add(3, "Aniseed Syrup", 10, 2); tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2); tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2); tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3); tbl.Rows.Add(48, "Chocolade", 12.75, 3); tbl.Rows.Add(49, "Maxilaku", 20, 3); return tbl; } #endregion } AjaxPro 支援直接返回 DataTable 和 DataView ,用戶端讀取方式同 DataSetvar tbl = res.value; // 直接存取 DataTable 需要注意的是,返回 DataView,實際上是返回 DataView 關聯的 DataTable 。