Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I created a very smiple service using VB.NET 2008 tring to interact with SQL server (adding one record to table) using the following code:

VB
Imports System.Data.SqlClient

Public Class Imsglobe_syncLocal

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
      
        Dim sqlstr As String
        Dim ds As New Global.System.Data.DataSet

        Dim constr As String = "Data Source=(local);Initial Catalog=InfoBank;Integrated Security=True"
        Dim Cmd As Global.System.Data.SqlClient.SqlCommand

        sqlstr = "INSERT INTO [dbo].[Root_tbl]  ([RootName] ,[UsrLevel]) VALUES ('testing',5)"
        Dim Con As Global.System.Data.SqlClient.SqlConnection = New Global.System.Data.SqlClient.SqlConnection(constr)
        Con.Open()
        Cmd = New Global.System.Data.SqlClient.SqlCommand(sqlstr, Con)
        Cmd.ExecuteNonQuery()
        Cmd.Dispose()
        Con.Close()

    End Sub


It seems this method is not working with the service.
Any hint will be appreciated.
Thanks in advance.
Posted
Updated 6-Jun-10 9:45am
v3
Comments
[no name] 5-Jun-10 13:10pm    
You'll have to give more than "not working". Is it not connecting to the database? Are you getting exceptions? Does the table exist in the database?
oss_au 6-Jun-10 9:39am    
hi Mark
thanks for reply.
put it this way, how to connect to SQL database and put one record in one table !!??
and to make things clear i test the code using windows forms but without Global name space i thought this name space might help, but seems im missing something whenever i try to build the service it wont built and it roll back.
thanks

1 solution

Try using an sql account and not integrated security.
it may be a permissions issue.
also say where you are getting an error and what it is .
having this work in a forms almost proves my point it is the authentication. if you get an error while in the service write to the error log what the error is and I will bet it is permissions. you can prove it by just change the connection string to a user maybe sa to prove it for now
 
Share this answer
 
v2
Comments
oss_au 6-Jun-10 9:29am    
hi Donsw
thanks for your reply, nothing wrong with the code.
the code works fine if im using a normal windows forms
i test everything before i implement it using the service.
when i build the service it just refuse to be built.
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900