To connect to a database you should create and use a SAConnection object.

For this tutorial we are going to connect to a local SQL Server database demo as a user guest with a password secret and then disconnect from it.

Connect to Database

The code below creates a connection object con represented by SAConnection class:

SAConnection con;
con.Connect( "demo", "guest", "secret", SA_SQLServer_Client );
The first parameter specifies the database, next two parameters specify the credentials, and the last one the type of the database connection is made to.
Remote Connection and Authentication
You can connect to remote servers using variety of protocols supported by each database server. You can also connect using different authentication mechanisms.
Choose your server for additional info

If you want to define the connection con as a connection to SQL Server database before calling SAConnection::Connect method use SAConnection::setClient method:

SAConnection con;
con.setClient( SA_SQLServer_Client );
con.Connect( "demo", "guest", "secret" );

Disconnect from Database

You can disconnect your connection object at any time with a call to the SAConnection::Disconnect method:

con.Disconnect();

It is safe to skip calling SAConnection::Disconnect method explicitly. Destructor SAConnection::~SAConnection will correctly close the connection if you didn't call SAConnection::Disconnect method before.

Need Help?
Send an e-mail to support@sqlapi.com if you want to ask code-level questions, if you’re stuck with a specific error or need advise on the library best practices.