// Stdafx. h
# Import " C: \ Program Files \ common files \ System \ ADO \ msado15.dll " No_namespace Rename ( " EOF " , " Adoeof " )
// MasterProgramInitialization Function
Bool cado2app: initinstance ()
{
Afxenablecontrolcontainer ();
Afxoleinit (); // initialize the com Library
//--------------------------------------------
The following is access: Hresult hr;
Try
{
HR = M_pconnection.createinstance ( " ADODB. Connection " ); /// Create a connection object
If (Succeeded (HR )){
HR = M_pconnection -> Open ( " Provider = Microsoft. Jet. oledb.4.0; Data Source = test. MDB " , "" , "" , Admodeunknown ); /// Connect to database
/// In the above sentence, the provider in the connection string is for the Access2000 environment. For Access97, you need to change it to provider = Microsoft. Jet. oledb.3.51 ;}
}
}
Catch (_ Com_error E) /// Capture exceptions
{
Cstring errormessage;
Errormessage. Format ( " An error occurred while connecting to the database! \ R \ n error message: % s " , E. errormessage ());
Afxmessagebox (errormessage ); /// Show error message
Return False;
}
The following links to SQL Server Cstring strsql;
Hresult hr;
Try
{
HR = M_pconnection.createinstance (_ uuidof (connection ));
M_pconnection -> Cursorlocation = Aduseclient;
Strsql = " Provider = sqloledb.1; Integrated Security = sspi; persist Security info = false; initial catalog = test; Data Source = yjm " ;
If (Succeeded (HR ))
{
HR = M_pconnection -> Open (_ bstr_t (strsql ), "" , "" , - 1 );
}
}
Catch (_ Com_error E) /// Capture exceptions
{
Cstring errormessage;
Errormessage. Format ( " An error occurred while connecting to the database! \ R \ n error message: % s " , E. errormessage ());
Afxmessagebox (errormessage ); /// Show error message
Return False;
}
// Afxmessagebox ("connected ~~ ");
Where:
----- ADO database connection string template for connecting to SQL Server ----------
Authentication Mode: "SQL Server and Windows"
Provider = sqloledb.1; persist Security info = true; user id = user name; Password = password; initial catalog = database name; Data Source = SQL Server Name
Authentication Mode: "Windows only"
Provider = sqloledb.1; Integrated Security = sspi; persist Security info = false; initial catalog = database name; Data Source = SQL Server Name