> For the complete documentation index, see [llms.txt](https://docs.proudnet.com/proudnet.eng/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.proudnet.com/proudnet.eng/proudnet/db_system/ado_api.md).

# ADO API

The <mark style="color:orange;">ADO Wrapper API</mark> 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.

{% hint style="info" %}
The code below is illustrated based on the [**git example project**](https://github.com/Nettention/ProudNet_Sample/tree/main/SimpleAdo).
{% endhint %}

## Connecting to DB

The syntax entered as a parameter to the Open function follows the [**Connection Strings**](https://www.connectionstrings.com/) rules.

```cpp
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

```cpp
// 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

{% tabs %}
{% tab title="MSSQL" %}

<pre class="language-cpp"><code class="lang-cpp">// CAdoCommand to hold the statement information 
<strong>Proud::CAdoCommand cmd;
</strong>// 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();
</code></pre>

{% endtab %}

{% tab title="MYSQL" %}

```cpp
// 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");

// Unlike MSSQL, you must call AppendParameter.
// Need to call AppendParameter instead of MSSQL.
cmd.AppendParameter(L"InUserID", ADODB::adVarWChar, ADODB::adParamInput, L"test", wcslen(L"test"));
cmd.AppendParameter(L"InPassword", ADODB::adVarWChar, ADODB::adParamInput, L"password", wcslen(L"password"));
cmd.AppendParameter(L"InCountry", ADODB::adInteger, ADODB::adParamInput, 1);

cmd.Execute();

rec.Close();
```

{% endtab %}
{% endtabs %}

### - Reading CAdoRecordset Data

{% tabs %}
{% tab title="MSSQL" %}

```cpp
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();
}
```

{% endtab %}

{% tab title="MYSQL" %}

```cpp
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 = rec.FieldValues[L"UserID"];
	strPassword = rec.FieldValues[L"Password"];
	strCountry = rec.FieldValues[L"Country"];
	
	wprintf(L"UserID : %s, Password : %s, Country : %s\n", strID.GetString(), strPassword.GetString(), strCountry.GetString());
	
	rec.MoveNext();
}
```

{% endtab %}
{% endtabs %}
