VS2010 Connection Database Operations (sqlserver2005/2008, access2007/2003, etc.)

Source: Internet
Author: User

1. First create a database file

2. Open vs2010/vs2012/vs2013

3. Build an engineering name called * * *

4. See Status bar: Tools--Connect to database


3.


4.


5. Click Advanced to copy the contents of DataSource and Provider to _bstr_t strconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=e :/graduation Project/houserealty/limite.mdb; Persist Security Info=false ";//access2007 (if you add ADO classes, here's how to add the ADO Class)


6. Add ADO class, Project---Add Class (add C + + class, not MFC-based class), named Cadoconn


7. Add code to the Adoconn class below

(1) First add code to the StdAfx.h file, import the ADO library

#ifndef _afx_no_afxcmn_support
#include <afxcmn.h>            //MFC for Windows Support for common controls
#import "C:\\Program Files\\Common Files\\system\\ado\\msado15.dll "No_namespace rename (" EOF "," adoeof ") Rename (" BOF "," Adobof ")
#endif//_afx_no_afxcmn_support
#include <afxcontrolbars.h>    //ribbon and Control bar MFC support
#endif
#endif
(2) to add in the ADOConn.h file:

Class Cadoconn
{
Public
Cadoconn (void);
~cadoconn (void);
_RecordsetPtr m_precordset;//Record set pointer
_connectionptr m_pconnection;
void Oninitadoconn ();//Connect Data source
_recordsetptr& GetRecordSet (_bstr_t bstrsql);//Get open recordset pointer
BOOL ExecuteSQL (_bstr_t bstrsql);//Execute SQL statement
void Exitconnect ();//Disconnect data connection
};

(3) Add in ADOConn.cpp

#include "StdAfx.h"
#include "ADOConn.h"




Cadoconn::cadoconn (void)
{
}




Cadoconn::~cadoconn (void)
{
}
void Cadoconn::oninitadoconn ()
{
:: CoInitialize (NULL);
Try
{
M_pconnection.createinstance ("ADODB. Connection ");
// _bstr_t strconnect= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=limite.mdb";//.mdb in the project directory access2003
_bstr_t strconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=e:/graduation project/houserealty/limite.mdb; Persist Security info=false ";//access2007
M_pconnection->open (StrConnect, "", "", adModeUnknown);

// AfxMessageBox ("Database connection succeeded");
}
catch (_com_error e)
{
AfxMessageBox ("Connection failed");
}


}
void Cadoconn::exitconnect ()
{
if (m_precordset!=null)//close record set and disconnect data source link
M_pconnection->close ();
:: CoUninitialize ();//release COM environment

}
_recordsetptr& Cadoconn::getrecordset (_bstr_t bstrsql)
{
Open Recordset, return recordset pointer
Try
{
if (m_pconnection==null)
Oninitadoconn ();
M_precordset.createinstance (__uuidof (Recordset));
M_precordset->open (Bstrsql,m_pconnection.getinterfaceptr (), adopendynamic,\
Adlockoptimistic,adcmdtext);
}
catch (_com_error e)
{
AfxMessageBox ("Record Data Open failed");
}
return m_precordset;

}
BOOL Cadoconn::executesql (_bstr_t bstrsql)
{
Try
{
if (m_pconnection==null)
Oninitadoconn ();
M_pconnection->execute (Bstrsql,null,adcmdtext);
return true;
}
catch (_com_error e)
{
AfxMessageBox ("Cannot open property data");
return false;
}
}

8. After the above steps, the VS connection database is almost ready to succeed.






















VS2010 Connection Database Operations (sqlserver2005/2008, access2007/2003, etc.)

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.