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;