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.