See Also
You are here: Interfacing > C++ Client Interface > Examples C++ Client Interface > Transaction
ContentsIndexHome
PreviousUpNext
Transaction
Scdriver Example

 

Simple Transactions 

This will connect to a server instance and initialize a database and table if they don't exist. Then it inserts 100 rows in a single transaction. 

This needs to be linked against scdriver.lib and run with scdriver.dll available. 

 

// Visual Studio added
#include "stdafx.h"

#include "scdriver.h"
#include <iostream>

int main(int argc, char** argv)
{
    CConnection conn;
    try
    {
        // Connect to the local machine on the default port
        conn.Connect("localhost",999);

        // Set up example database and create table
        conn.Execute("CREATE DATABASE IF NOT EXISTS scdriver_example");
        conn.GetCompletionStatus();
        conn.Execute("USE scdriver_example");
        conn.GetCompletionStatus();
        conn.Execute("CREATE TABLE IF NOT EXISTS example_run ("
                        "id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, "
                        "added DATETIME NOT NULL PARTITION, "
                        "description VARCHAR(255))");
        conn.GetCompletionStatus();

        // Begin a transaction with the default read-committed isolation level
        conn.BeginTransaction();

        // Insert a bunch of rows
        for(int i = 0; i < 100; i++)
            conn.Execute("INSERT INTO example_run VALUES "
                         "(0,GETDATE(),'Example transaction')");

        conn.CommitTransaction();

        // Must be called after every query to finish the query status
        conn.GetCompletionStatus();
    }
    catch (node_exception& e)
    {
        std::cerr << "Error: " << e.msg << std::endl;
        return 1;
    }
    return 0;
}

 

 

 

Related