Windows/MFC
A set of ADO classes - version 2.20 By Carlos Antollini (ADODB)
aucd29
2013. 10. 2. 17:58
Overview
I created these classes to make it easy to work with ADO. For this I created the CADODatabse class and the CADORecordset class.
Connection Sample
[code]
//Sample with Connection string for SQL Server
CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = _T("");
strConnection = _T("Provider=MSDASQL;PersistSecurityInfo=False;"
"Trusted_Connection=Yes;"
"Data Source=Access Sql Server;catalog=sampledb");
pAdoDb->SetConnectionString(strConnection);
if(pAdoDb->Open())
DoSomething();
.
.
.
//Sample with Connection String for Access database
CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = _T("");
strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\VCProjects\\ADO\\Test\\dbTest.mdb");
pAdoDb->SetConnectionString(strConnection);
if(pAdoDb->Open())
{
DoSomething();
.
.
.
pAdoDb->Close();
}
delete pAdoDb;
[/code]
CADODatabase::Execute
The Execute function executes a SQL statement in the open database.
[code]
BOOL Execute(LPCTSTR lpstrExec);
throw(CADOException);
[/code]
Parameter
[code]
<PRE>CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = _T("");
strConnection = _T("Provider=MSDASQL;"
"PersistSecurityInfo=False;"
"Trusted_Connection=Yes"
"Data Source=Access Sql Server;catalog=sampledb");
if(pAdoDb->Open(strConnection))
pAdoDb->Execute("Delete From tblClients Where Cheker = 3");</PRE>
[/code]
The function returns TRUE if it was executed successfully
CADODatabase::GetRecordsAffected
The GetRecordsAffcted function returns the number of records affected by the last SQL statement executed.
[code]int GetRecordsAffected();[/code]
For Example
[code]
<PRE>CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = "";
strConnection = _T("Provider=MSDASQL;"
"PersistSecurityInfo=False;Trusted_Connection=Yes
Data Source=Access Sql Server;catalog=sampledb");
if(pAdoDb->Open((LPCTSTR)strConnection))
{
CString strQry = _T("");
int numRecords;
strQry.Format(_T("sp_StoreClientFields_ps '%s', %d"),
(LPCTSTR)strParam1, nParam2);
CADORecordset* pRs = new CADORecordset(pAdoDb);
if(!pRs->Open((LPCTSTR)strQry))
{
delete pRs;
delete pAdoDb;
return FALSE
}
numRecords = pRs->GetRecordCount();
while(!pRs->IsEof())
{
CString strVal = _T("");
int nVal = 0;
//Get Numeric Field Value
pRs->GetFieldValue("NumField1", nVal)
//Get String Field Data
pRs->GetFieldValue("StrField..", strVal)
DoSomething(nVal, strVal);
pRs->MoveNext();
}
pRs->Close();
}
else
return FALSE;</PRE>
[/code]
For Example2
[code]
<PRE lang=sql><B>//SQL SCRIPT...</B>
Create Procedure sp_OutputTest
@IN1 int,
@OutInt int Output,
@OutChar varchar(20) Output
As
SELECT
@OutChar = 'Hello World'
SELECT
@OutInt = 10 * @IN1
return (90)
GO
<B>//Visual C++ Code...</B>
CADOParameter pParamRetVal(CADORecordset::typeInteger, sizeof(int),
CADOParameter::paramReturnValue);
CADOParameter pParamIn(CADORecordset::typeInteger, sizeof(int));
CADOParameter pParamOutInt(CADORecordset::typeInteger, sizeof(int),
CADOParameter::paramOutput);
CADOParameter pParamOutChar(CADORecordset::typeChar, sizeof(char) * 20,
CADOParameter::paramOutput);
pParamIn.SetValue(2);
CADOCommand pCmd(&pDb, "sp_OutputTest");
pCmd.AddParameter(&pParamRetVal);
pCmd.AddParameter(&pParamIn);
pCmd.AddParameter(&pParamOutInt);
pCmd.AddParameter(&pParamOutChar);
CADORecordset* prs = new CADORecordset(pDb);
if(pRs->Execute(&pCmd))
{
int nVal = 0;
int nRetVal = 0;
CString str = _T("");
pParamRetVal.GetValue(nRetVal);
pParamOutInt.GetValue(nVal);
pParamOutChar.GetValue(str);
}
delete pRS;</PRE>
[/code]
출처 : http://www.codeproject.com/database/caaadoclass1.asp
I created these classes to make it easy to work with ADO. For this I created the CADODatabse class and the CADORecordset class.
Connection Sample
[code]
//Sample with Connection string for SQL Server
CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = _T("");
strConnection = _T("Provider=MSDASQL;PersistSecurityInfo=False;"
"Trusted_Connection=Yes;"
"Data Source=Access Sql Server;catalog=sampledb");
pAdoDb->SetConnectionString(strConnection);
if(pAdoDb->Open())
DoSomething();
.
.
.
//Sample with Connection String for Access database
CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = _T("");
strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\VCProjects\\ADO\\Test\\dbTest.mdb");
pAdoDb->SetConnectionString(strConnection);
if(pAdoDb->Open())
{
DoSomething();
.
.
.
pAdoDb->Close();
}
delete pAdoDb;
[/code]
CADODatabase::Execute
The Execute function executes a SQL statement in the open database.
[code]
BOOL Execute(LPCTSTR lpstrExec);
throw(CADOException);
[/code]
Parameter
[code]
<PRE>CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = _T("");
strConnection = _T("Provider=MSDASQL;"
"PersistSecurityInfo=False;"
"Trusted_Connection=Yes"
"Data Source=Access Sql Server;catalog=sampledb");
if(pAdoDb->Open(strConnection))
pAdoDb->Execute("Delete From tblClients Where Cheker = 3");</PRE>
[/code]
The function returns TRUE if it was executed successfully
CADODatabase::GetRecordsAffected
The GetRecordsAffcted function returns the number of records affected by the last SQL statement executed.
[code]int GetRecordsAffected();[/code]
For Example
[code]
<PRE>CADODatabase* pAdoDb = new CADODatabase();
CString strConnection = "";
strConnection = _T("Provider=MSDASQL;"
"PersistSecurityInfo=False;Trusted_Connection=Yes
Data Source=Access Sql Server;catalog=sampledb");
if(pAdoDb->Open((LPCTSTR)strConnection))
{
CString strQry = _T("");
int numRecords;
strQry.Format(_T("sp_StoreClientFields_ps '%s', %d"),
(LPCTSTR)strParam1, nParam2);
CADORecordset* pRs = new CADORecordset(pAdoDb);
if(!pRs->Open((LPCTSTR)strQry))
{
delete pRs;
delete pAdoDb;
return FALSE
}
numRecords = pRs->GetRecordCount();
while(!pRs->IsEof())
{
CString strVal = _T("");
int nVal = 0;
//Get Numeric Field Value
pRs->GetFieldValue("NumField1", nVal)
//Get String Field Data
pRs->GetFieldValue("StrField..", strVal)
DoSomething(nVal, strVal);
pRs->MoveNext();
}
pRs->Close();
}
else
return FALSE;</PRE>
[/code]
For Example2
[code]
<PRE lang=sql><B>//SQL SCRIPT...</B>
Create Procedure sp_OutputTest
@IN1 int,
@OutInt int Output,
@OutChar varchar(20) Output
As
SELECT
@OutChar = 'Hello World'
SELECT
@OutInt = 10 * @IN1
return (90)
GO
<B>//Visual C++ Code...</B>
CADOParameter pParamRetVal(CADORecordset::typeInteger, sizeof(int),
CADOParameter::paramReturnValue);
CADOParameter pParamIn(CADORecordset::typeInteger, sizeof(int));
CADOParameter pParamOutInt(CADORecordset::typeInteger, sizeof(int),
CADOParameter::paramOutput);
CADOParameter pParamOutChar(CADORecordset::typeChar, sizeof(char) * 20,
CADOParameter::paramOutput);
pParamIn.SetValue(2);
CADOCommand pCmd(&pDb, "sp_OutputTest");
pCmd.AddParameter(&pParamRetVal);
pCmd.AddParameter(&pParamIn);
pCmd.AddParameter(&pParamOutInt);
pCmd.AddParameter(&pParamOutChar);
CADORecordset* prs = new CADORecordset(pDb);
if(pRs->Execute(&pCmd))
{
int nVal = 0;
int nRetVal = 0;
CString str = _T("");
pParamRetVal.GetValue(nRetVal);
pParamOutInt.GetValue(nVal);
pParamOutChar.GetValue(str);
}
delete pRS;</PRE>
[/code]
출처 : http://www.codeproject.com/database/caaadoclass1.asp