Click here to Skip to main content
15,886,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I created windows service to transfer data from local database to SQL Server through internet but the connection always failed although the same connection works very will in Windows Form Application, Is Windows Service need special connection string or configuration to connect to sql server or I missing something

anyone can help me
---------------------------------------------------
Hi again

Thanks for all your reply but I still have the same problem the connection work in windows application but didn't work in Services Application , kindly find below the code hope that any one can help me


Service Account:
LocalService

connection string
name="myconn" providername="System.Data.SqlClient" connectionstring="Data Source=serverip\SQLEXPRESS;
Initial Catalog=database; User ID=username; Password=mypassword"

Code
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["myconn"];
SqlConnection destConnection = new SqlConnection(settings.ToString ());
destConnection.Open();


Exception
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Posted
Updated 5-Nov-20 1:18am
v2
Comments
Dylan Morley 18-May-12 6:26am    
Any errors \ exception messages?

My best guess would be that you're using windows authentication to connect to SQL?

If this is the case, you need to make sure the user under which the service is running has permissions within SQL.

If you look in System, Administrative Tools, Services and find your service. You can adjust the credentials associated with it.

When you run a windows form application the security context under which it is run is that of the user who's logged in. That security context doesn't persist when you move the code to a service.

Often when we install services they install against the Network Service or Local System security principles. These shouldn't be given access to SQL.

You could also create an SQL user and use a connection string which implements SQL authentication instead.
 
Share this answer
 
Comments
Shery.md 18-May-12 8:44am    
Thanks for your reply
I am ready use SQL authentication but still have exception
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
check below points.
1. If you are define your connection string in app.config under <appSettings>
Then please try to define it in <Configuration> section under <ConnectionString>.

2. If you have already done points one then check that the reference of system.Configuration is given to service or not.

3. if you have also already done point no. 2 then check that your are not try to getting the value of connection string out of function, i mean to say out of constructor or whatever.

If all above 3 points are ok, then
Can you paste your both connection code in this dash board?

After it i will help you.
Because i have created many windows service just like your approach.
and i think i will help you.

Mahesh Patel
MCA-2010
 
Share this answer
 
v3
Comments
Shery.md 18-May-12 8:24am    
First of all thanks for your reply

I checked all above but still have problem to connect the server

please find below my connection code


connection string
+++++++++++++++++
name="myconn" providername="System.Data.SqlClient" connectionstring="Data Source=serverip\SQLEXPRESS;
Initial Catalog=database; User ID=username; Password=mypassword"

Code
+++++
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["myconn"];

SqlConnection destConnection = new SqlConnection(settings.ToString ());

destConnection.Open();


Exception
+++++++++++
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
raja.dinakaran.c.r 29-Nov-12 6:06am    
I am unable to pass the connection string value as you said,From the app config it is getting passed as null value to the service.
/////////////////////
Using System.Configuration;
/////////////////////////////
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
///////////////////////////////////////////

<configuration>
<add name="Connectionstring" connectionstring="Data Source=Servername; Initial Catalog=DBName;User ID=sa;Password=password">

Please Reply .

Congrats in in advance.
Thank you.
Typically, a service runs under "Local System" account. That account cannot access any network / internet resources. Change the user for your service to a user with network access rights.
 
Share this answer
 
hi dear,

Please check below point also.

go to sql server configuration manager.

Now, Under SQL Server Network configuration Manager -
Check that the -
Named Pipes = Enabled
and TCP/IP = Enabled

And user string declaration insted of
C#
ConnectionStringSettings


Just like
C#
string settings =Convert.ToString(ConfigurationManager.ConnectionStrings["myconn"]);
 
Share this answer
 
You dont have to use anything specificaly for connecting to db from Windows Service.It seems to be the problem with your Sql Server. Can you please try login to that same sql server directly. Please let me know if you are able to login to the server directly. if you are not able to login to the server directly there will be some connectivity issue with DB server. You can figure it out with your DB Admin.

Thanks,
Lijo
 
Share this answer
 
Comments
Shery.md 4-Jun-12 1:05am    
thanks Lijo for your reply
Yes, I can login to the server directly and even when using add new data source from the dot net it give me the connection Succeeded but I still have the same exception
Do you have any idea about the problem

Thanks,
Shery
homa rahmani 17-Mar-14 6:21am    
i have a problem like this.
i made a windows service to send SMS on special times with reading data from SQL.
there is no error.
but after a install service does not work.
i do not know what to do?
thanks for your help
Add port numbers at Friewall to allow Ms SQL Server
Reference this https://msdn.microsoft.com/en-us/library/cc646023(v=sql.120).aspx
 
Share this answer
 
Comments
Dave Kreskowiak 5-Oct-16 23:03pm    
You dug up a FOUR YEAR OLD question that has already been answered.
Right Click on the service select properties -> Log On Select this account and provide credentials you use as per connection string
 
Share this answer
 
Comments
Richard Deeming 5-Nov-20 10:14am    
As already adequately explained in solution 1. And again in solution 3. Both posted eight years ago.

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