Solved

Connecting to SQL from a vb.net service...

Posted on 2006-06-15
8
337 Views
Last Modified: 2008-01-16
Hi,

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)

tnx
~j

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

Service cannot be started. System.IO.FileNotFoundException: Could not load file or assembly 'Interop.ADODB, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'Interop.ADODB, Version=2.8.0.0, 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=127.0.0.1;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
        cn.Close()
        rs.Close()
------------ END CODE -------------------
0
Comment
Question by:NCSO
  • 5
  • 3
8 Comments
 

Author Comment

by:NCSO
Comment Utility
Quick comment!

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

~j
0
 
LVL 11

Expert Comment

by:MacNuttin
Comment Utility
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:
http://www.connectionstrings.com/
0
 

Author Comment

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

tnx
~j
0
 

Author Comment

by:NCSO
Comment Utility
Hi,

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.

tnx
~j
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 11

Expert Comment

by:MacNuttin
Comment Utility
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;"
oSQLConn.Open()
0
 

Author Comment

by:NCSO
Comment Utility
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...

tnx
~j
0
 
LVL 11

Accepted Solution

by:
MacNuttin earned 500 total points
Comment Utility
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
0
 

Author Comment

by:NCSO
Comment Utility
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!

tnx
~j
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now