?? 18-1 access數據庫瀏覽器.hta
字號:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=GB2312" />
<title>18-1 Access數據庫瀏覽器</title>
<style>
* { font-size:12px; font-family:宋體, Arial; } /*規定了所有的字體樣式*/
body { overflow:auto; background-color:buttonface; border-style:none; }
input { border-width:1px; height:17px; margin-right:2px; padding:0px; }
</style>
<script>
//通用函數部分
//函數IsNull判斷給出的參數是否是null值或空字符串
function IsNull(o){ if(o==null||o==""){return(true);}else{return(false);} }
//函數HandError用于顯示腳本執行中的錯誤信息
function HandError(e,o){
if(IsNull(o)){o="Global";}
var ErrInfo=o+"-Error : "+e.description+"\n";
ErrInfo+="\nDetail Info :"
for(var ii in e)ErrInfo+="\n\t"+ii+" : "+e[ii];
alert(ErrInfo);
}
//數據庫操作部分函數
//函數“OpenDatabase”用于打開指定路徑的Access數據庫
function OpenDatabase(strDBPath){
//Access數據庫的默認路徑
var DEFAULT_DATA_FILE="TestData.mdb";
//創建“Connection”控件對象
var oConn=new ActiveXObject("ADODB.Connection");
//判斷是否缺省路徑參數
if(IsNull(strDBPath)){strDBPath=DEFAULT_DATA_FILE;}
//定義與數據庫的鏈接字符串
var strProvider="Provider=Microsoft.Jet.OLEDB.4.0;"
var strDBPath="Data Source=" +strDBPath+";"
var strConn=strProvider+strDBPath
//試圖打開一個對指定數據庫的連接
try{
oConn.Open(strConn,"","");
//返回連接對象
return(oConn);
}catch(e){HandError(e,"OpenDataBase");return(false);}
}
//獲取數據庫中的表的名稱
function GetTableName(oConn){
//生成一個新的記錄集對象
var oRSSchema=new ActiveXObject("ADODB.Recordset");
//初始化返回值
var ret="";
//判斷如果未給出連接參數則返回
if(IsNull(oConn)){return(false);}
try{
//獲取數據庫的信息摘要
oRSSchema=oConn.openSchema(20);
//將記錄集指針指向其頭部
oRSSchema.movefirst();
//循環讀取摘要信息
while(!oRSSchema.EOF){
//如果相應字段為“表格”
if(oRSSchema("TABLE_TYPE")=="TABLE"){
//記錄表格名稱
ret+=oRSSchema("TABLE_NAME")+"\n";
}
oRSSchema.movenext();
}
//關閉摘要的記錄集
oRSSchema.Close();
return(ret);
}catch(e){HandError(e,"GetTableName");return(false);}
}
//函數“GetRecordset”按照指定的SQL語句返回執行后的數據集對象
function GetRecordset(strSQL,strDBPath,PageSize,PageNo){
//如果SQL語句為空則返回
if(IsNull(strSQL)){return(false);}
//初始化分頁
if(isNaN(PageSize)){PageSize=20;}
if(isNaN(PageNo)){PageNo=1;}
//生成一個新的記錄集對象
var oRecordset=new ActiveXObject("ADODB.Recordset");
//打開指定路徑的數據庫
var oConn=OpenDatabase(strDBPath);
//如果打開失敗的錯誤處理
if(!oConn){alert("Can not open database file !");return(false);}
try{
//依照指定的SQL語句打開數據庫
oRecordset.open(strSQL,oConn,1);
//執行分頁
oRecordset.PageSize=PageSize;
oRecordset.AbsolutePage=PageNo;
//返回執行后的結果記錄集
return(oRecordset);
}catch(e){HandError(e,"GetRecordset");return(false);}
}
//數據庫的HTML表現部分函數
//函數“UpdateTableList”用于更新顯示的數據庫中的表名
function UpdateTableList(){
try{
//根據輸入的文件路徑打開指定的數據庫文件
var oConn=OpenDatabase(txtDataFile.value);
//如果打開失敗則停止執行,報告錯誤并返回
if(!oConn){alert("Can not open database file !");return(false);}
//獲取數據庫中的表名
var ss=GetTableName(oConn);
ss=ss.split("\n");
//清除下拉列表框的內容
ClearSelect(txtTableName);
//將獲得的數據庫表名添加到下拉列表框
for(var ii=0;ii<ss.length;ii++)AddToSelect(txtTableName,ss[ii],ss[ii],ii);
//關閉到數據庫的連接
oConn.Close();
//更新數據區的顯示內容
UpdateGrid();
}catch(e){HandError(e,"UpdateTableList");return(false);}
}
//函數“UpdateGrid”用于顯示指定的數據頁
function UpdateGrid(PageNo){
try{
//初始化分頁變量,當前頁“PageNo”變量缺省值為1
var strTable="",PageSize=25,PageNo=isNaN(PageNo)?1:PageNo;
//定義默認的SQL查詢語句
var strSQL="Select * From [";
strSQL+=txtTableName.value+"]";
//如果輸入了篩選條件,則將其寫入SQL語句中
if(txtWhere.value!=""){strSQL+="WHERE "+txtWhere.value;}
//將完整的SQL語句顯示為窗口標題
top.document.title=strSQL;
//獲取符合條件的數據集對象
var oRecordset=GetRecordset(strSQL,txtDataFile.value,PageSize,PageNo);
//如果數據集為空則停止執行
if(!oRecordset||oRecordset.EOF){ClearContent(DataGrid);return(false);}
//輸出總記錄數和總分頁數
txtTotleRecords.innerText=oRecordset.RecordCount;
txtTotlePages.innerText=oRecordset.PageCount;
//清空數據輸出區域
ClearContent(DataGrid);
//輸出表頭
var Row=0;
var NewRow=DataGrid.insertRow(Row);
for(var Col=0;Col<oRecordset.Fields.Count;Col++){
NewCell=NewRow.insertCell(Col);
//輸出單元格內容為該記錄的域名
NewCell.innerText=oRecordset.Fields(Col).Name;
}
Row++;
//循環記錄集,按分頁大小輸出指定數量的記錄
while(!oRecordset.EOF&&Row<=PageSize){
NewRow=DataGrid.insertRow(Row);
for(var Col=0;Col<oRecordset.Fields.Count;Col++){
NewCell=NewRow.insertCell(Col);
NewCell.innerText=IsNull(oRecordset.Fields(Col))?" ":oRecordset.Fields(Col);
}
Row++;
oRecordset.MoveNext();
}
//關閉記錄集
oRecordset.Close();
}catch(e){HandError(e,"UpdateGrid");return(false);}
}
//在當前表中跳轉到指定分頁
function GoToPage(n){
n=isNaN(n)?1:n;
n=parseInt(n);
if(n<1){
n=1;
}
if(n>parseInt(txtTotlePages.innerText)){
n=parseInt(txtTotlePages.innerText);
}
txtPageNo.value=n;
UpdateGrid(n);
}
//HTML的DOM操作
//向下拉列表框中插入指定的條目
function AddToSelect(oSelect,Name,Value,intIndex){
if(IsNull(Name)){return(false);}
var oOption=new Option(Name,Value);
intIndex=IsNull(intIndex)?oSelect.options.length:intIndex;
oSelect.options[intIndex]=oOption;
}
//清空下拉列表框
function ClearSelect(oSelect){
while(oSelect.options.length>0){
oSelect.removeChild(oSelect.options[0]);
}
}
//清空HTML元素內的所有子元素
function ClearContent(obj1){
try{
for(var ii=0;ii<obj1.children.length;ii++){
obj1.removeChild(obj1.children[ii]);
}
}catch(e){HandError(e,"ClearContent");return(false);}
}
</script>
</head>
<body>
源文件位置: <input size=40 onChange="UpdateTableList();" id="txtDataFile">
<input type=button onclick="txtFileBrowser.click();txtDataFile.value=txtFileBrowser.value;txtDataFile.onchange();" value="瀏覽"><input type=file value="testData.mdb" size=40 style="display:none;" id="txtFileBrowser">
<hr>
Table Name : <select id="txtTableName" style="width:200px;" onchange="UpdateGrid();"></select>
Where <input size=40 id="txtWhere" onchange="txtPageNo.onchange();">
<hr>
<div style="Width:100%;height:65%;border:#000 1px solid;overflow:auto;margin-bottom:0px;">
<table id=DataGrid border=1 cellspacing=0 cellpadding=1px >
</table>
</div>
<div style="border:#000 1px solid;width:100%;">
<input id="cmdBack" type=button onclick="GoToPage(1);" value="<<" style="margin-right:0px;">
<input id="cmdBack" type=button onclick="var temp1=txtPageNo.value-1;GoToPage(temp1);" value="<" style="margin-right:0px;">
<input id="txtPageNo" onchange="GoToPage(this.value);" value="1" size=6 style="text-align:center;margin-right:0px;">
<input id="cmdForward" type=button onclick="var temp1=parseInt(txtPageNo.value)+1;GoToPage(temp1);" value=">" style="margin-right:0px;">
<input id="cmdBack" type=button onclick="GoToPage(txtTotlePages.innerText);" value=">>" style="margin-right:0px;">
<b>Totle Pages : </b><label id="txtTotlePages">0</label> / <b>Totle Recordes : </b><label id="txtTotleRecords">0</label>
</div>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -