實現原理: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;