Connect to SQL Server with SQLAPI++

471 Views Asked by At

I am trying to write a C++ script, on a remote Ubuntu machine, that would connect to a database in order to retrieve some data. I created my database on Microsoft SQL Server on an other remote machine.

I have installed SQLAPI++ and read the SQL server guide and the SQL Server (ODBC) Guide to try to connect without any success. I think the problem might be in the connect function but not sure why and I haven't found any examples to learn what I've been doing wrong.

The first time I saw the connect function being used was in this way:

con.Connect ("test",    // database name
             "tester",  // user name
             "tester",  // password
             SA_SQLServer_Client); //SQL Server Client

But I need to somehow give the IP adress of the machine where my database is stored. So I found the following few lines on (https://www.sqlapi.com/ApiDoc/mssql_odbc/) which allows me to pass on the IP of my DB in the sDBString.

void Connect(
    const SAString &sDBString, 
    const SAString &sUserID, 
    const SAString &sPassword, 
    SAClient_t eSAClient = SA_Client_NotSpecified);

Here is the code I have been using:

#include<stdio.h>
#include<SQLAPI.h>
int main(int argc, char* argv[])
{
    // create connection object to connect to database
    SAConnection con;
    try
    {
        // connect to database
        // in this example, it is Oracle,
        // but can also be Sybase, Informix, DB2
        // SQLServer, InterBase, SQLBase and ODBC

        con.Connect(
                    const SAString &"[<16.0.0.1>][<SDB>][;<TrustServerCertificate=yes>]",
                    const SAString &"sa", 
                    const SAString &"****", 
                    SAClient_t eSAClient = SA_SQLServer_Client);
        printf("We are connected!\n");
  
        // Disconnect is optional
        // autodisconnect will occur in destructor if needed
        con.Disconnect();
        printf("We are disconnected!\n");
    }
  
    catch(SAException & x)
    {
        // SAConnection::Rollback()
        // can also throw an exception
        // (if a network error for example),
        // we will be ready
        try
        {
            // on error rollback changes
            con.Rollback ();
        }
        catch(SAException &)
        {
        }
        // print error message
        printf("%s\n", (const char*)x.ErrText());
    }
}

When trying to compile I get these errors:

ConnectToSQLServer.cpp:16:21: error: expected primary-expression before ‘const’
   16 |                     const SAString &"[<16.0.0.1>][<SDB>][;<TrustServerCertificate>]",
      |                     ^~~~~
ConnectToSQLServer.cpp:17:21: error: expected primary-expression before ‘const’
   17 |                     const SAString &"sa",
      |                     ^~~~~
ConnectToSQLServer.cpp:18:21: error: expected primary-expression before ‘const’
   18 |                     const SAString &"SyAd@CoTe1",
      |                     ^~~~~
ConnectToSQLServer.cpp:19:32: error: expected primary-expression before ‘eSAClient’
   19 |                     SAClient_t eSAClient = SA_SQLServer_Client);
      |                                ^~~~~~~~~

Thank you for your time.

Update :

The error was solved and now I got a new one !

undefined reference to `SAConnection::SAConnection()'

This has to do with linking libraries in c++ which looks very complicated and I don't really understand what it means. To get to use the SQLAPI.h I included it with its path (#include "/path/SQLAPI.h") like I would do an import in python since I was to afraid to get into linking libraries.

I would really appreciate some explanation on why what I did was wrong or how to solve this issue.

Thank you for your time.

2

There are 2 best solutions below

0
Alex P On

Your Connect function should be something like:

con.Connect("[<16.0.0.1>][<SDB>][;<TrustServerCertificate=yes>]","sa", "****", SA_SQLServer_Client);
0
Thanh Nguyen On

Your connection string should look like this:

con.Connect("192.168.50.24\SQLINSTANCENAME@MyDatabaseName", "username", "12345", SA_SQLServer_Client);

You can replace the IP with "localhost" or "(localdb)". The "@" character is mandatory but "TrustServerCertificate" is not necessary.