ODBC API
As with the ADO API, you can also use ODBC to access the DB.
The description below is based on the git example code.
Object Definitions
// The object to establish the connection with
Proud::COdbcConnection conn;
// The object to set the command on
Proud::COdbcCommand cmd;
// The object to hold the query results
Proud::COdbcRecordset record;
// There is no need to use it as an object to contain query errors.
Proud::COdbcWarnings warnings;
Connecting to DB
The syntax entered as a parameter to the Open function follows the Connection Strings rules.
conn.Open(Proud::String::NewFormat(L"Driver={ODBC Driver 17 for SQL Server};Server=localhost;Trusted_Connection=yes;"), &warnigs);
conn.Open(Proud::String::NewFormat(L"Driver={MySQL ODBC 8.0 Unicode Driver};Server=127.0.0.1;Port=3306;Uid=proud;Pwd=proudnet123;Option=3;"));
Using DB
- Using Query
conn.BeginTrans();
// Example 1
conn.Execute(_PNT("CREATE TABLE test(id int primary key, name nvarchar(30))"));
// Example 2
Proud::String query = _PNT("INSERT INTO test(id) VALUES(1)");
result = conn.Execute(query);
conn.CommitTrans();
// Example 1
conn.Execute(_PNT("CREATE TABLE test(id int primary key, name varchar(30))"));
// Example 2
Proud::String query = _PNT("INSERT INTO test(id) VALUES(1)");
result = conn.Execute(query);
- Using Query prepare
int id;
Proud::String name;
cmd.Prepare(conn, _PNT("INSERT INTO test(id, name) VALUES(?, ?)"));
cmd.AppendInputParameter(&id);
cmd.AppendInputParameter(&name);
id = 5;
name = _PNT("Nettention");
result = cmd.Execute();
- Using stored procedures
int ret;
int id;
Proud::String name;
// If only an input parameter is present
cmd.Prepare(conn, _PNT("{call insertTest(?,?)}"));
cmd.AppendInputParameter(&id);
cmd.AppendInputParameter(&name);
id = 8;
name = _PNT("SPTest");
result = cmd.Execute();
// If the output parameter is present
cmd.Prepare(conn, _PNT("{? = call outputTest(?,?)}"));
cmd.AppendOutputParameter(&ret);
cmd.AppendInputParameter(&id);
cmd.AppendOutputParameter(&name);
id = 5;
name.GetBuffer(100);
result = cmd.Execute();
name.ReleaseBuffer();
int id;
Proud::String name;
// input
cmd.Prepare(conn, _PNT("call insertTest(?,?)"));
cmd.AppendInputParameter(&id);
cmd.AppendInputParameter(&name);
id = 8;
name = _PNT("SPTest");
result = cmd.Execute();
// output
cmd.Prepare(conn, _PNT("call outputTest(?,?)"));
cmd.AppendInputParameter(&id);
cmd.AppendOutputParameter(&name);
id = 5;
name.GetBuffer(100);
result = cmd.Execute();
name.ReleaseBuffer();
- Reading data with Recordset
int id;
Proud::String name;
result = conn.Execute(record, _PNT("SELECT * FROM test"));
while (record.MoveNext())
{
id = record.GetFieldValue(_PNT("id"));
name = record.GetFieldValue(_PNT("name"));
_tprintf_s(_PNT("id = %d, name = %ws\n"), id, name.GetString());
}
Last updated