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

 实现原理:SQLServer数据库连接是使用SQL Server实现的。

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

再使用“ADODB.Recordset”对象,通过查询一张已存在的表Select name from sysdatabases where name='master',判断是否成功。

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

  

源码:

 

prototype BOOL DB_CheckSQLServerCon(BYREF STRING,STRING,BYREF STRING,BYREF STRING,BYREF STRING);
//============================================================//
//function : DB_CheckSQLServerCon //
// //
//purpose: 测试SQL Server是否可连接 //
// //
//Argment: svServerName-SQL Server连接服务名称 //
// svUserName-登录帐号 //
// svUserPassword-登录密码 //
// szError-错误提示信息 //
//return: TRUE -连接成功 FALSE - 连接失败 //
//Remark: 本验证使用 SQL Server 验证 //
//============================================================//
function BOOL DB_CheckSQLServerCon(svServerName,szDatabase,svUserName, svUserPassword,szError)
OBJECT pADOConnObj, pADORecordSetObj;
STRING szADOConnObjID, szADORecordSetObjID, szConnString, szSQL,svDriver;
BOOL bExists;
begin
bExists = FALSE;
svDriver = "SQL Server";
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 + "server=" + svServerName + ";";
szConnString = szConnString + "uid=" + svUserName + ";";
szConnString = szConnString + "pwd=" + svUserPassword + ";";
if(szDatabase !="") then
szConnString = szConnString + "database=" + szDatabase + ";";
endif;

// Open the ADO Connection
pADOConnObj.Open(szConnString);
pADOConnObj.Close();
bExists = TRUE;

/*// 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 name from sysdatabases where name='" + "master" + "'";

// Use the recordset to see if the database exists
pADORecordSetObj.Open(szSQL);
if (pADORecordSetObj.RecordCount = 1) then
bExists = TRUE;
endif; */
catch
//nErr =Err.Number;
Sprintf(szError,"Error!Number:%d;",Err.Number);
szError = szError +"Desc:"+Err.Description;
bExists = FALSE;
//Clean up
set pADOConnObj = NOTHING;
endcatch;
set pADOConnObj = NOTHING;
return bExists;
end;

posted @ 2012-05-11 11:10  ☆会飞的鱼☆  阅读(1151)  评论(0编辑  收藏  举报