Solved

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

Posted on 2006-06-15
8
360 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
ID: 16914160
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
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:
http://www.connectionstrings.com/
0
 

Author Comment

by:NCSO
ID: 16914994
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:NCSO
ID: 16919431
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
 
LVL 11

Expert Comment

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

Author Comment

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

tnx
~j
0
 
LVL 11

Accepted Solution

by:
MacNuttin earned 500 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
0
 

Author Comment

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

tnx
~j
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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