📄 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 + -