473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SQLConnection.Open() in a WebService

I'm experiencing a strange problem that has me baffled. I created a
webservice with a webmethod that connects to a remote MS SQL Server.
It works fine when connecting to one server (running SQL Server 7).
But it can't connect to another server (running SQL Server 2000). The
SQLConnection.Open() call results in a long pause, then the error
"could not connect to server". What's odd is that if I put the exact
same SQLConnection code into a new Windows application project it
works with either server.

What could be making the SQL connection fail when it's in a webservice
compared to a Windows app? And why only with the one server?

Any help would be greatly appreciated!
Nov 16 '05 #1
3 7896
Hi stephen
Just a note before hitting the issue , you should be using SqlConnection
with SQL server 2000 and an OleDbConnection with SQL server 7. ( I guess
you already have that , if not then try to have SqlConnection with the 2000
as this may be the problem )
Another thing you may be looking at is the credentials of the user that
access the database. If you are using windows authentication , then mostly
this is the source of the problem. You said the connection worked form
windows app but not from the service. The windows application by default
run under the credentials of the logged on user . whoever this is no the
default behavior of the service.
Normally windows services run under an account that has minimum privileges
on the system. Try to run the service under a user account that you are
logged on with , as this mostly is the cause of the problem. To do so open
the services window ( control panel ' Administered tools ' services .
right click your service and select properties , then select the logon tab
and choose this account ( you many need to enter the username and the
password ).. hope that helps

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2

Thanks for the reply Mohamed. I'm using SqlConnection with the SQL 2000
server. The login is a user defined on the SQL Server:

SqlConnection mySQLConnection = new SqlConnection();
mySQLConnection.ConnectionString="data source=192.168.30.44;
uid=rocky;pwd=raccoon;database=myDatabase";
mySQLConnection.Open();

This code works fine in the Windows app with either SQL Server 7 or
2000. It also works fine in the webservice app with one server, which
happens to be SQL Server 7. It does not work with the other server,
running SQL Server 2000. I can connect to either database using
Enterprise Manager.

I looked in the Services control panel and do not see my webservice
there. I'm a little confused by your suggestion -- I didn't think
webservices were the same as other services.

Still stuck :-/

-Stephen
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Hi Steve
ahaaa , sorry I thought your using windows service so all what I said about
how to make it run under your account would not valid . However, still
the source of the problem would be the same . either services ( web ,
windows ) work with a minimum privileges account . however it is a
different story if you you want to change that for the web service . if
you want to impersonate user with your web service ( so it would have
enough privileges to connect ) you would need to pass to its proxy class (
the one that you are using for reference from your application ) a
credentials object . if you want to pass the credentials of the user of the
application , you would pass the default credentials object . Take a look
at that snippet I once had
if(chkDefault.Checked){

s.Credentials = System.Net.CredentialCache.DefaultCredentials;

}
else{
//WindowsImpersonationContext impersonatedUser =
WindowsIdentity.GetCurrent().Impersonate();

//System.Net.NetworkCredential myCred1
=System.Net.CredentialCache.DefaultCredentials;
System.Net.NetworkCredential myCred = (System.Net.NetworkCredential)
System.Net.CredentialCache.DefaultCredentials;//
System.Net.NetworkCredential(txtuserid.Text,txtpas sword.Text,txtdomain.Text)
;
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
myCache.Add(new Uri(s.Url), txttype.Text, myCred);

MessageBox.Show(WindowsIdentity.GetCurrent().Name) ;
MessageBox.Show(WindowsIdentity.GetCurrent().IsAno nymous.ToString());
MessageBox.Show(System.Threading.Thread.CurrentPri ncipal.Identity.IsAuthenti
cated.ToString());
s.Credentials = myCache;
}
You might need to read about the System.Net.CredentialCache a bit on MSDN

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
4
by: arran.pearce | last post by:
Hi, I am using .NET 2 (beta2) and are having problems with using a SqlConnection. I have created a test application that opens a sql connections, gets some basic data and then closes the...
4
by: The Coolest Dolphin | last post by:
Hi all, I have a question/problem concerning usage of my sqlconnection throughout my program. In my main form (frmMain) I defined an sqlconnection (I've set the connection public so that I can...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
0
by: Alex | last post by:
Hi! I have previously designed class that would read encrypted connection from registry, create a connection object (sql, oracle, oledb...) and return it to the calling class. I had to change this...
2
by: axel22 | last post by:
Ok here it goes. 1. I've created a new web service project in F:\c#\WebSites\ServisBaze. 2. I've published the web service with IIS in a virtual directory who's actual path is:...
4
by: Victor | last post by:
Hi Guys I have a problem here. I want to improve the performance for a website. When I looked into the system, I have found that the system made the "SqlConnection Object" static. That mean only...
5
by: fniles | last post by:
I am using VB.NET 2003. When using SQLClient.SQLConnection with SQL 2005 database, 1. connection pooling is automatically used, right ? I mean, in the connection string I do not need to explicitly...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.