InstallShield進階應用程式–測試SQL Server是否可串連

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.