Connect to the database using MFC

Source: Internet
Author: User

 

In MFC, there are two main methods to connect to the SQL database.
1. Connect using ADO:
# Import "msado15.dll" no_namespace Rename ("EOF", "endoffile ")
// This dll must be imported. This file is usually stored in the C:/program files/common files/system/ADO path.
_ Connectionptr m_ptrconnection; // database connection object
Add the following statement to the constructor:
M_ptrconnection = NULL;
: Coinitialize (null );
// Main code for connecting to the database
Bool datavisitor: connectdatabase (_ bstr_t connectionstr)
{
/*
Added by stone. If idoconnection has not been set up, then create one.
*/
If (m_ptrconnection = NULL)
{
Hresult hR = m_ptrconnection.createinstance (_ uuidof (connection ));
If (failed (HR ))
{
Return false;
}
Else
{
_ Bstr_t strconnect = connectionstr;
// "Provider = sqloledb; server = (local); database = navigation; uid = sa; Pwd = 3277625 ;";

M_ptrconnection-> cursorlocation = aduseclient;
M_ptrconnection-> isolationlevel = adxactreadcommitted;
Try
{
M_ptrconnection-> open (strconnect, "", "", admodeunknown );
Return true;
}
Catch (_ com_error E)
{
// Afxmessagebox (char *) E. Description ());
Return false;
}

}
}
Return true;
}

2. Use ODBC to connect
# Include <afxdao. h>
Cdaodatabase * mydatabase;

Bool mydb_opersql: open_mydatabase (cstring connstr)
{
Try
{
If (mydatabase = NULL)
{
Mydatabase = new cdaodatabase ();
}
Mydatabase-> open (null, 0, 0, connstr );

}
Catch (cdaoexception * E)
{
Cstring message = _ T ("mydb_opersql database exception :");
Message + = e-> m_perrorinfo-> m_strdescription;
Char info [400];
Sprintf (Info, message );
Disperrormessage (Info ,__ line __);
E-> Delete ();
Return false;
}
Catch (cmemoryexception * E)
{
Disperrormessage ("mydb_opersql Memory exception! ",__ Line __);
E-> Delete ();
Return false;
}
Catch (...)
{
Disperrormessage ("mydb_opersql other exceptions! ",__ Line __);
Return false;
}
Return true;
}
The connection string connstr is generally the following content:
"ODBC; driver = {SQL Server}; server = (local); database = yourdatabase; uid = yourid; Pwd = yourpassword"

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.