Solved

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

Posted on 2006-06-15
8
382 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
[X]
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
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 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