Using MySQl with VC++
-
Hi, I intend to make a sample application in VC++ to using MySQL databases. Can anybody suggest me where and how to start? I am new to databases any help would be great. Thanks in Advance!! Regards, Tony
Visit this page http://www.codeproject.com/KB/database/[^] and set the language filter to C++ and skill to Beginner. There are plenty of articles that may give you a lead to begin with. Note that there would be nothing specific to MySQL, but they will all speak about databases in general.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
Hi, I intend to make a sample application in VC++ to using MySQL databases. Can anybody suggest me where and how to start? I am new to databases any help would be great. Thanks in Advance!! Regards, Tony
It yours: http://files.myopera.com/nguyenbinh07/files/MySQL.rar[^] Have funs!!!
-
Hi, I intend to make a sample application in VC++ to using MySQL databases. Can anybody suggest me where and how to start? I am new to databases any help would be great. Thanks in Advance!! Regards, Tony
Simple 7 Steps :-D
1)First of all import msado15.dll into your workspace
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")2)Initialize COM libraries using
CoInitialize(NULL);
- Create connection & record set pointers
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
- Open data base
#define STR_DATABASE L"DRIVER={sql server};SERVER=serverName;Database=datbaseName;" L"UID=sa; PWD=sa;"
HRESULT hr = S_OK;
hr = pConn->Open(STR_DATABASE, L"", L"", adOpenUnspecified);
if(FAILED(hr))
{
AfxMessageBox ("Error instantiating Connection object\n");
}5)Execute Query and read values
try
{
CString strSQL(“Select * From MyTable”);
pRst->Open( _variant_t(strSQL),_variant_t((IDispatch *) pConn, true),adOpenDynamic, adLockReadOnly,adCmdText);if(!pRst->EndOfFile){
CString strValue = (char*)(_bstr_t)pRst->Fields->Item[“FieldName”]- >Value;int nValue = (long)pRst->Fields->Item[“FieldName”]->Value;
// Here “FieldName” is the column name of table. You will get the value of this column in the corresponding variable.
}
catch(_com_error &ce)
{
AfxMessageBox(ce.ErrorInfo);
OR
AfxMessageBox(ce.Description());}
6)Close connection and record set pointers
if(pRst->State == adStateOpen)
pRst->Close();
if(pConn->State == adStateOpen)
pConn->Close();7)UnInitialized COM library
::CoUninitialize();
-
Simple 7 Steps :-D
1)First of all import msado15.dll into your workspace
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")2)Initialize COM libraries using
CoInitialize(NULL);
- Create connection & record set pointers
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
- Open data base
#define STR_DATABASE L"DRIVER={sql server};SERVER=serverName;Database=datbaseName;" L"UID=sa; PWD=sa;"
HRESULT hr = S_OK;
hr = pConn->Open(STR_DATABASE, L"", L"", adOpenUnspecified);
if(FAILED(hr))
{
AfxMessageBox ("Error instantiating Connection object\n");
}5)Execute Query and read values
try
{
CString strSQL(“Select * From MyTable”);
pRst->Open( _variant_t(strSQL),_variant_t((IDispatch *) pConn, true),adOpenDynamic, adLockReadOnly,adCmdText);if(!pRst->EndOfFile){
CString strValue = (char*)(_bstr_t)pRst->Fields->Item[“FieldName”]- >Value;int nValue = (long)pRst->Fields->Item[“FieldName”]->Value;
// Here “FieldName” is the column name of table. You will get the value of this column in the corresponding variable.
}
catch(_com_error &ce)
{
AfxMessageBox(ce.ErrorInfo);
OR
AfxMessageBox(ce.Description());}
6)Close connection and record set pointers
if(pRst->State == adStateOpen)
pRst->Close();
if(pConn->State == adStateOpen)
pConn->Close();7)UnInitialized COM library
::CoUninitialize();
Can i go like this as well ========================================================================================= try { CDaoDatabase* pCDaoDb = NULL; CDaoRecordset* pPointsSet = NULL; pCDaoDb = new CDaoDatabase(); pCDaoDb->Open("C:\\temp\\sample2.mdb", FALSE, FALSE, ""); pPointsSet = new CDaoRecordset(pCDaoDb); if (!pPointsSet->IsOpen()) { pPointsSet->Open(dbOpenTable, "STUDENT", 0); } COleVariant oleVariant; oleVariant = pPointsSet->GetFieldValue("STUDENTID"); long lTemp = oleVariant.lVal; oleVariant = pPointsSet->GetFieldValue("Address"); CString strTemp = oleVariant.pcVal; CString strTemp2 = strTemp.AllocSysString(); int nCount = pPointsSet->GetRecordCount(); //pPointsSet-> } catch(CDaoException* e) { //int nError = e->ReportError(); CString error = e->m_pErrorInfo->m_strDescription; MessageBox( NULL, error, "ERROR", MB_OK ); } =========================================================================================
-
Can i go like this as well ========================================================================================= try { CDaoDatabase* pCDaoDb = NULL; CDaoRecordset* pPointsSet = NULL; pCDaoDb = new CDaoDatabase(); pCDaoDb->Open("C:\\temp\\sample2.mdb", FALSE, FALSE, ""); pPointsSet = new CDaoRecordset(pCDaoDb); if (!pPointsSet->IsOpen()) { pPointsSet->Open(dbOpenTable, "STUDENT", 0); } COleVariant oleVariant; oleVariant = pPointsSet->GetFieldValue("STUDENTID"); long lTemp = oleVariant.lVal; oleVariant = pPointsSet->GetFieldValue("Address"); CString strTemp = oleVariant.pcVal; CString strTemp2 = strTemp.AllocSysString(); int nCount = pPointsSet->GetRecordCount(); //pPointsSet-> } catch(CDaoException* e) { //int nError = e->ReportError(); CString error = e->m_pErrorInfo->m_strDescription; MessageBox( NULL, error, "ERROR", MB_OK ); } =========================================================================================
Ya definitely!