ADO API

The ADO Wrapper API in ProudNet DB is an API designed to easily handle Microsoft ActiveX Data Object (ADO) in C++ language, making it easier to access DBMS using ADO.

The code below is illustrated based on the git example project.

Connecting to DB

The syntax entered as a parameter to the Open function follows the Connection Strings rules.

Proud::CAdoConnection conn;

// MSSQL
conn.Open(L"Driver={ODBC Driver 17 for SQL Server};Server=localhost;Database=ProudAdo-Test;Trusted_Connection=yes;");
// MYSQL
conn.Open(L"Driver={MySQL ODBC 8.0 Unicode Driver};Server=127.0.0.1;Port=3306;Database=proudado-test;User ID=proud;Password=proudnet123;Option=3;", Proud::DbmsType::MySql);

Using DB

- Using Query directly

// A query for testing.
// To use this query, 
// a UserData table with columns named UserId, Password,
// and Country must exist in the DB.


// Transaction starts
conn.BeginTrans();

// Query execution code
// The example below is a query for testing purposes.
// To use this query, 
// a UserData table with columns named UserId, Password, 
// and Country must exist in the DB.
conn.Execute(Proud::String::NewFormat(L"insert into UserData (UserID, Password, Country) VALUES(%s, %s, %d)", L"test", L"password", 1));

// Transaction commit
conn.CommitTrans();

- Using stored procedures

// CAdoCommand to hold the statement information 
Proud::CAdoCommand cmd;
// CAdoRecordset to hold the return data
Proud::CAdoRecordset rec;

// A procedure called AddUserData must exist.
cmd.Prepare(conn, L"AddUserData");

// Add a parameter.
cmd.Parameters[1] = L"test";
cmd.Parameters[2] = L"password";
cmd.Parameters[3] = 1;

// Execute the procedure
cmd.Execute(rec);

rec.Close();

- Reading CAdoRecordset Data

Proud::AdoRecordset rec;
// Similarly, the UserData table must exist in the DB.
rec.Open(conn, Proud::DbOpenFor::OpenForReadWrite, L"select * from UserData");

Proud::String strID, strPassword, strCountry;
while (rec.IsEOF() == false)
{
	strID = ((Proud::String)rec.FieldValues[L"UserID"]).GetString();
	strPassword = ((Proud::String)rec.FieldValues[L"Password"]).GetString();
	strCountry = ((Proud::String)rec.FieldValues[L"Country"]).GetString();
	
	wprintf(L"UserID : %s, Password : %s, Country : %s\n", strID.GetString(), strPassword.GetString(), strCountry.GetString());
	
	rec.MoveNext();
}

Last updated