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:
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.
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.