InstallShield高级应用--测试Access是否可连接

实现原理:Access数据库连接是使用Microsoft Access Driver (*.mdb)实现的。

通过Access连接数据库字符串,使用“ADODB.Connection“对象,连接到Access数据库,

再使用“ADODB.Recordset”对象,通过查询一张已存在的表TBL_test,判断是否成功。

注意:当前电脑必包含ADODB.Connection和ADODB.Recordset这二个对象。

  

源码:

prototype BOOL DB_CheckAccessConn(STRING,NUMBER,BYREF STRING);
//============================================================//
//function : DB_CheckAccessConn //
// //
//purpose: 测试Access是否可连接 //
// //
//Argment: szDBPath:Access数据库绝对路径 //

//return: TRUE -连接成功 FALSE - 连接失败 //
//Remark: 本验证使用 Microsoft Access Driver (*.mdb) 验证 //
// DRIVER={Microsoft Access Driver (*.mdb)}; //
// DBQ=数据库名.mdb;UID= sa;PWD=sa//
//============================================================//

function BOOL DB_CheckAccessConn(szDBPath,szError)
OBJECT pADOConnObj, pADORecordSetObj;
STRING szADOConnObjID, szADORecordSetObjID, szConnString, szSQL,svDriver;
STRING svServerName,szDatabase,svUserName, svUserPassword;
BOOL bExists;
begin
bExists = FALSE;
svDriver = "Microsoft Access Driver (*.mdb)";
try
// Create ADO Connection Object to connect to the SQL server
szADOConnObjID = "ADODB.Connection";
set pADOConnObj = CreateObject(szADOConnObjID);

// Create the SQL string to complete the connection
szConnString = "driver={" + svDriver + "};";
szConnString = szConnString + "DBQ=" + szDBPath + ";";
szConnString = szConnString + "uid=sa;";
szConnString = szConnString + "pwd=sa;";

// Open the ADO Connection
pADOConnObj.Open(szConnString);


// Create ADO Recordset object for the return
szADORecordSetObjID = "ADODB.Recordset";
set pADORecordSetObj = CreateObject(szADORecordSetObjID);

// Set some ADO Recordset properties
pADORecordSetObj.CursorType = 3;
pADORecordSetObj.ActiveConnection = pADOConnObj;

// Create the SQL string to retrieve the database if it exists
szSQL = "SELECT * FROM TBL_test";

// Use the recordset to see if the database exists
pADORecordSetObj.Open(szSQL);
if (pADORecordSetObj.RecordCount >= 0) then
bExists = TRUE;
endif;

pADORecordSetObj.Close();
pADOConnObj.Close();
catch
//nErr =Err.Number;

Sprintf(szError,"Error!Number:%d;",Err.Number);
szError = szError +"Desc:"+Err.Description;
szError ="DB:"+ szError;

bExists = FALSE;
//Clean up
set pADOConnObj = NOTHING;
set pADORecordSetObj = NOTHING;
endcatch;
set pADOConnObj = NOTHING;
set pADORecordSetObj = NOTHING;
return bExists;
end;

你可能感兴趣的:(Install)