How to connect Access and SQL Server using VC + ADO

Source: Internet
Author: User

 

// 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

 

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.