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

 

Prepared Query 

This will connect to the default server instance and create the example database and table, if necessary. Then it will make a prepared query to simply insert a row. 

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

 

// used by Microsoft Visual Studio Projects
#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 and procedure
        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();

        // Build the stored query for running multiple times
        CCommand prepared_query;

        // Prepare the query with the parameter
        prepared_query.PrepareText(&conn,"INSERT INTO example_run VALUES (0,getdate(),?message)");

        // Set parameter and run the query
        prepared_query.AddParameter("message",DB_VARCHAR,"One prepared query text");
        prepared_query.Execute();
        conn.GetCompletionStatus();

        // Reset parameter and run the query again
        prepared_query.AddParameter("message",DB_VARCHAR,"Another prepared query text");
        prepared_query.Execute();
        conn.GetCompletionStatus();
    }
    catch (node_exception& e)
    {
        std::cerr << "Error: " << e.msg << std::endl;
        return 1;
    }
    return 0;
}

 

 

 

Related