/****Function Lib By Hutia********************
This function lib is writen by hutia.
Version 2.17
Build on 28/11/2005
**********************************************/
/****Function List************************
Packed functions:
DB
adCmdText, addColumn, alterTable, createTable, dropColumn, dropTable, execSQL,
getFieldName, getRecordset, getRecordsetEx, getTableName, openDatabase,
*********************************************/
//***packed in the variable DB****
var DB =
{
adCmdText:1,
addColumn:
function(tableName,fieldName,conn,typeName){
if(isEmpty(typeName)){typeName=" Text";}
strSQL="Alter Table ["+tableName+"] Add ["+fieldName+"] "+typeName;
execSQL(strSQL,conn);
},
alterTable:
function(strAlterTableStructure,conn){
//strAlterTableStructure should in this Form:
/*tableName {ADD {COLUMN field type[(size)] [NOT NULL] [CONSTRAINT index] |
ALTER COLUMN field type[(size)] |
CONSTRAINT multifieldindex} |
DROP {COLUMN field I CONSTRAINT indexname} }
*/
strSQL="Alter Table "+strAlterTableStructure;
execSQL(strSQL,conn);
},
createTable:
function(strTableStructure,conn){
//strTableStructure should in this Form:
/*tableName ( $fieldName fieldType [(size)] [NOT NULL]
[CONSTRAINT index] [WITH COMPRESSION | WITH COMP] [index1]
[, field2 type [(size)] [NOT NULL] [index2] [, ...]]
[, CONSTRAINT multifieldindex [, ...]]))
*/
strSQL="Create Table "+strTableStructure;
execSQL(strSQL,conn);
},
dropColumn:
function(tableName,fieldName,conn){
strSQL="Alter Table "+tableName+" Drop COLUMN ["+fieldName+"] ";
execSQL(strSQL,conn);
},
dropTable:
function(tableName,conn){
strSQL="Drop Table "+strTableStructure;
execSQL(strSQL,conn);
},
execSQL:
function(strSQLCommand,conn){
try{
var sqlCommand=new ActiveXObject("ADODB.Command");
with(sqlCommand){
CommandText=strSQLCommand;
CommandType=adCmdText;
Prepared=false;
ActiveConnection=conn;
}
return(sqlCommand.Execute());
}catch(e){handError(e,"execSQL");}
},
getFieldName:
function(conn,tableName){
var oRSSchema=new ActiveXObject("ADODB.Recordset");
var ret="";
if(isNull(conn)){return(false);}
try{
oRSSchema=conn.openSchema(4);
oRSSchema.movefirst();
while(!oRSSchema.EOF){
if(oRSSchema("TABLE_NAME")==tableName){
ret+=oRSSchema("COLUMN_NAME")+"\n";
}
oRSSchema.movenext();
}
oRSSchema.Close();
return(ret);
}catch(e){handError(e,"getFieldName");return(false);}
},
getRecordset:
function(strSQL,strDBPath,PageSize,PageNo){
if(isNull(strSQL)){return(false);}
if(isNaN(PageSize)){PageSize=20;}
if(isNaN(PageNo)){PageNo=1;}
var oRecordset=new ActiveXObject("ADODB.Recordset");
var conn=this.openDatabase(strDBPath);
if(!conn){alert("Can not open database file !");return(false);}
try{
oRecordset.open(strSQL,conn,1);
if(oRecordset.EOF){return(false);}
oRecordset.PageSize=PageSize;
oRecordset.AbsolutePage=PageNo;
return(oRecordset);
}catch(e){handError(e,"GetRecordset");return(false);}
},
getRecordsetEx:
function(strSQL,strDBPath){
if(isNull(strSQL)){return(false);}
var oRecordset=new ActiveXObject("ADODB.Recordset");
var conn=this.openDatabase(strDBPath);
if(!conn){alert("Can not open database file !");return(false);}
try{
oRecordset.open(strSQL,conn);
return(oRecordset);
}catch(e){handError(e,"GetRecordset");return(false);}
},
getTableName:
function(conn){
var oRSSchema=new ActiveXObject("ADODB.Recordset");
var ret="";
if(isNull(conn)){return(false);}
try{
oRSSchema=conn.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);}
},
openDatabase:
function(strDBPath,strDBPass){
conn=new ActiveXObject("ADODB.Connection");
if(isNull(strDBPath)){strDBPath=DEFAULT_DATA_FILE;}
var strProvider="Provider=Microsoft.Jet.OLEDB.4.0;"
var strDBPath="Data Source=" +strDBPath+";"
if(!isEmpty(strDBPass)){
strDBPass="Jet OLEDB:Database Password="+strDBPass+";";
}else{
strDBPass="";
}
var strConn=strProvider+strDBPath+strDBPass;
try{
conn.Open(strConn,"","");
return(conn);
}catch(e){handError(e,"OpenDataBase");return(false);}
}
};
posted on 2007-06-06 00:58
汪杰 阅读(247)
评论(0) 编辑 收藏 引用 所属分类:
javascript