Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Connecting to SQL from a service...

Posted on 2006-06-15
Medium Priority
Last Modified: 2008-01-16

I am banging my head into the keyboard... please help!

I have a program which does some local and remote calls to SQL databases...  It works great when it is in a regular program.

I am trying to port it over to run in a service, but I get the error I have listed below... and below the error is the code I am running in the service...

The database I am trying to connect to is a local install of SQL-EXPRESS.  

If it is a rights issue how do I get around that? (i figured it might not be because of the file not found error)


-------- ERROR FROM EVENTLOG -------------

Service cannot be started. System.IO.FileNotFoundException: Could not load file or assembly 'Interop.ADODB, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'Interop.ADODB, Version=, Culture=neutral, PublicKeyToken=null'
   at CrimeManager.Service1.SQLStuff()
   at CrimeManager.Service1.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

------------ ERROR END ----------------

----------- CODE RUNNING IN SERVICE -----------------
        Dim cn As New ADODB.Connection()
        Dim rs As New ADODB.Recordset()
        Dim cnStr As String
        cnStr = "Provider=SQLOLEDB;Initial Catalog=Reporting;Data Source=;User ID=sa;Password=joesbodega;"
        cn.Open(cnStr) 'Open SQL connection
        rs.Open("select * from tblAuthor", cnStr)
        While Not rs.EOF
            My.Computer.FileSystem.WriteAllText("c:\test.log", rs.Fields.Item("Username").Value, True)
        End While
------------ END CODE -------------------
Question by:NCSO
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3

Author Comment

ID: 16914160
Quick comment!

I tried launching the service with both my user-account AND the local admin account to no avail...

LVL 11

Expert Comment

ID: 16914921
From the error it looks like the type of connection is needing a driver that isn't there maybe in a regular program there is a different driver being called for SQLOLEDB than for the service have you tried the SQLOLEDB.1

There are more tips here:

Author Comment

ID: 16914994
tried the .1 but that didn't do it, I'll look through the url in the morning... time to go home :)

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 16919431

I doesn't even get to the connection string.  It complains in the first line (Dim cn As New ADODB.Connection())...  

I am thinking it's a permisson issue (it is working in a non-service) but ... how do I specify what user context to use?  

The properties for the ServiceProcessInstaller gives me a way of specifying the account to use (LocalService, Network Service, LocalSystem and User), I've tried all four with varions non-working result.

LVL 11

Expert Comment

ID: 16920739
Try this to import first the driver I changed loopback to local - no reason

Imports System.Data.SqlClient
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString="Data Source=(local);Initial Catalog=Reporting;User ID=sa;Password=joesbodega;"

Author Comment

ID: 16935210
That does work but if I the open a recordset connection with adodb it crashes...

I guess I'll have to rewrite all my code to use the datareader since that seems to work better...

LVL 11

Accepted Solution

MacNuttin earned 2000 total points
ID: 16935265
I try to code one connection object and call it everywhere in my code so if I have to change the password or the datasourse it's all in one spot - just a tip. Anyway I'm glad it worked for you

Author Comment

ID: 16936921
Well, it is not the connection string I am worried about, it's the code...

Here's the problem:

We have a bunch of pc's in cars that write data to a local db where they are stored until their 3G cards connect to the central database back home and then a process (the service I'm trying to write) will write it back to the central database.   So once the connection is established, I read the local rows in the db and write them up to the central db.  

The old way I did it, I can do this:  strSQL = "INSERT INTO tblAuthor (AuthorID, FullName, FirstName, LastName, Other) VALUES (rs.fields.item("AuthorID").value, rs.fields.item("FullName").value, rs.fields.item("FirstName").value, rs.fields.item("LastName").value, rs.fields.item("Other").value, connection)

With the datareader i'd have to do this:

strSQL = "INSERT INTO tblAuthor (AuthorID, FullName, FirstName, LastName, Other) VALUES (dr(1).ToString(), dr(2).ToString(), dr(3).ToString(), dr(4).ToString(), dr(5).ToString(), connection)

And although this seem like heaven for some people it makes for ugly unreadable code.  The line above is a simplified example, some of our tables are huge (one has 231 fields) which would make this approach near impossible to read and maintain.  (imagine having to remove a certain field?)

I'll obviously write a piece of code to create the insert statement, but I am not going to want to look at that string again.  Is there a way to write the field-names instead if their index?

Anyway, you got me on the right path to get it working, so enjoy the points and have a great day...  I appreciate your help!


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question