Connecting to SQLServer db from C++ console app using SQLAPI

8.9k Views Asked by At

I would like to connect to a SQLServer db from my console c++ application. I have used Microsoft SQL Server to create a db called "Test". I also have "installed" the sqlapi from sqlapi.com. For starters I just want to connect to that db using a modified sample code that ships along with sqlapi.

#include <stdio.h>  // for printf
#include <SQLAPI.h> // main SQLAPI++ header

int main(int argc, char* argv[])
{
    SAConnection con; // create connection object
    printf("Howdy!\n");
    try
    {
        con.Connect("Test","COMPNAME\Username","Pwd", SA_SQLServer_Client); // database name//  user name //password

        printf("We are connected!\n");

        // Disconnect is optional
        // autodisconnect will ocur 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());
    }

    return 0;
}

It compiles fine, no errors but it fails to connect. I know a little more than the basics in c++, but SQL I started with kind of yesterday. So I have some questions about connecting:

  • During the installation of SQL server 2008 express I created a new Windows user account called Username with password Password
  • When trying to connect, is it enough to give the db name "Test" or has it to be Test.db or similar. For the username, do I have to Write Computername\Username to connect or just "Username"
  • I've ticket a "Start automatic" box when installing SQLserver, and in the taskamanager I see some sql thing running. Is that THE server, or do I have to fire it up manually someway before trying to connect?
  • Do I have to be logged in as the user under which account I created the db, or is it enough to know the Username and Password?
  • The Authentication method is "Windows Authentication"

I think you may understand where the trouble is, it these famous first steps....(Well it took me quite some time before I figured out how to set up a project in VS to...;-) ) If you have some answers, or hints on where I can find them (a nice book or link) I would be very thankful.

4

There are 4 best solutions below

0
On

SQLAPI.h is used to connect to mysql database, as you mentioned you trying to connect to sqlserver you need to use ss6API.h instead http://sqlapi.com/ServerSpecific/SQLServer_DbLibrary.html#Getting%20native%20SQL%20Server%20API**

0
On

If you are using Windows Authentication, you do not need to pass the username and password. You will need to set up the database properly to allow anyone "select" privileges. I believe the generic user in SQL Server is called 'guest', and is probably configured by default to allow that.

The really issue you are having, I believe, is that you need to pass the server name and database name together, like:

con.Connect("SQLEXPRESS@Test","","", SA_SQLServer_Client);

or possibly

con.Connect("ELVIS\SQLEXPRESS@Test","","", SA_SQLServer_Client);

All you need to know is here, you just read through it carefully.

2
On

When connecting to SQL, the database name is enough. What you need to know is the server, the SQL instance name, and the database. In a default installation, the default instance is used which means you don't need to specify the instance name. Normally you'll see the service is installed as SQL Server (MSSQLSERVER).

You do not need to use the username of the account you used to create the database, but the permissions have to be there for any other user. For testing purposes at least, using the name you installed/created with should give you proper permission to access the database.

If you don't know what user to use, or which permissions are in place, you need to check with Management Studio. This is the link for SQL Server Management Studio 2008 Expres if you don't have it installed already:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=08e52ac2-1d62-45f6-9a4a-4b76a8564a2b

That will let you login, view the databases, and check what security is in place.

0
On
con.Connect("SQLEXPRESS@Test","","", SA_SQLServer_Client);

After "@" put the name of the database.