InstallShield advanced application-test whether SQL Server can be connected

Source: Internet
Author: User

Implementation principle: SQL Server is used for SQL Server database connection.

Connect to the database string through sqlserver and use the "ADODB. Connection" object to connect to the access database,

Use the "ADODB. recordset" object to query an existing table, select name from sysdatabases where name = 'master', and determine whether the table is successful.

Note: The current computer must contain the ADODB. Connection and ADODB. recordset objects.

Source code:

 

prototype bool db_checksqlservercon (byref string, String, byref string );
// ========================================== ==========================================///
// function: db_checksqlservercon //
///
// purpose: test whether the SQL Server can be connected. //
///
// argment: svservername-SQL Server Connection Service name //
// svusername-Logon account //
// svuserpassword-Logon password //
// szerror-error message information //
// return: true-connection successful false-Connection Failed //
// remark: use SQL Server for verification //
// ========================== ==============================================//< br> function bool db_checksqlservercon (svservername, szdatabase, svusername, svuserpassword, szerror)
Object padoconnobj, padorecordsetobj;
string szadoconnobjid, credential, 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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.