# ADO API

ProudNet DB中的<mark style="color:orange;">ADO Wrapper API</mark>是爲了在C++語言中輕鬆處理Microsoft ActiveX Data Object(ADO)而製作的API,可以更容易地利用ADO訪問DBMS。

{% hint style="info" %}
下面的程式碼是基於[**git 範例專案**](https://github.com/Nettention/ProudNet_Sample/tree/main/SimpleAdo)進行解釋的。
{% endhint %}

## 連接DB

作爲參數進入Open函數的語句遵循[**Connection Strings**](https://www.connectionstrings.com/)規則。

```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);
```

## 使用DB

### - 直接使用Query

```cpp
// 這是一個測試查詢。
// 爲了使用該查詢
// DB 有名為 UserId、Password 和 Country 的欄位
// UserData 表必須存在。


// 開始交易
conn.BeginTrans();

// 查詢執行程式碼
// 下面的範例是用於測試目的的查詢。
// 爲了使用該查詢
// DB 有名為 UserId、Password 和 Country 的欄位。
// UserData 表必須存在。
conn.Execute(Proud::String::NewFormat(L"insert into UserData (UserID, Password, Country) VALUES(%s, %s, %d)", L"test", L"password", 1));

// 交易 commit
conn.CommitTrans();
```

### - 使用Stored procedure

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

<pre class="language-cpp"><code class="lang-cpp">// 包含命令信息的 CAdoCommand
<strong>Proud::CAdoCommand cmd;
</strong>// 包含 Return 數據的 CAdoRecordset
Proud::CAdoRecordset rec;

// 必須存在名爲AddUserData的procedure。
cmd.Prepare(conn, L"AddUserData");

// 添加參數 。
cmd.Parameters[1] = L"test";
cmd.Parameters[2] = L"password";
cmd.Parameters[3] = 1;

// 運行 procedure
cmd.Execute(rec);

rec.Close();
</code></pre>

{% endtab %}

{% tab title="MYSQL" %}

```cpp
// 包含命令信息的 CAdoCommand
Proud::CAdoCommand cmd;
// 包含 Return 數據的 CAdoRecordset
Proud::CAdoRecordset rec;

// 必須存在名爲AddUserData的procedure。
cmd.Prepare(conn, L"AddUserData");

// 與MSSQL不同,必須呼叫Append Parameter。
// 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 %}

### - 讀取 CAdoRecordset 數據

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

```cpp
Proud::AdoRecordset rec;
// 同樣，DB中必須存在UserData表。
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;
// 同樣，DB中必須存在UserData表。
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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.proudnet.com/proudnet.cn/proudnet/db_system/ado_api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
