views:

357

answers:

2

Hi folks,

I'm trying to write a service in CBuilder 6 (target XP Pro). Part of this service's job is to monitor and update a table on a database. We use direct ODBC to connect to the database, but the problem is happening with ADO as well, so we'll use that for simplicities sake.

You can see my code below. This is called from a function which is executed in the ServiceStart event.

My issue is, I'm not able to get a connection. Our MSSQL DB uses domain authentication, so I have tried running the service using my domain account. I've also tried explicitly defining my domain\username and password but that doesn't work either (in the properties for the service, log on tab).

Can anyone at all help me with this? Anything at all is appreciated.

Another question I have is how would one debug a service? I have remote debugger installed. Do I spawn the process using something like rundll32.exe or something? Again, any help appreciate.

Many thank in advance

Stu.

Code Snippet:

TADOConnection* DB = new TADOConnection(this);
  try
  {
    DB->ConnectionString = "Provider=MSDASQL.1;Password=password;Persist Security Info=True;User ID=usernamej;Data Source=datasource";
    DB->Connected = true;

    returnValue = DB->Connected;
    ShowMessage("Connected");
  }
  catch (Exception &exception)
  {
    ShowMessage("Not COnnected");
  }

Notes: The connectionstring property is indicative only and has had the username, password and database changed. Using this string in a standalone application results in a consistently successful connection and data can be fetched using a query.


In Response to JP:

Hi JP.

Thanks for your comment. I'm actually not getting an exception thrown here (which is strange). I modified the code (see below) to include a few more msgboxes and the only one I see is the first one - attempting connection.

ShowMessage("Attempting Login"); 
TADOConnection* DB = new TADOConnection(NULL); 
try { 
  ShowMessage("1"); 
  DB->ConnectionString = "<as before>"; 
  ShowMessage("2"); 
  DB->Connected = true; 
  ShowMessage("Connected"); 
} catch (Exception &exception) { 
  ShowMessage(exception.Message); 
  ShowMessage("Not COnnected"); 

}

Any ideas? I cant see why the connection component isn't even being created (note I tried changing the owner from this to NULL as well just to see what would happen).

Thanks!

Stu.

A: 

What you are running into is the Windows Service permissions problems.

  • Essentially, services live in a lowered permissions environment.

  • I suggest you log to the Event Log.

  • Generated windows will only be displayed if you run it as Local Service with Interactive Desktop enabled.

Paul Nathan
Thanks for the suggetions. I've been looking into how to use the event log, but so far haven't had a great deal of luck actually writing to it. I'm just experimenting now with it. Having the DB locally has solved this problem for me. The DB was always going to be with the service anyway (I wasn't aware of this at the time).
Rooj
+1  A: 

If you are using domain authentication, you shouldn't have a user name or password in your connection string:

Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=True;

Or an appropriate variant from http://www.connectionstrings.com/sql-server-2005

Your computer will need to be a member of the domain and the database server will need to trust that domain. The service will need to be running under a account under the domain of which the computer is a member.

Cade Roux
Hi. Thanks for the comments. This is using domain authentication, but I was experimenting with the connection string to see if I had to specify the user being at service level now. Cheers!
Rooj