Click to See Complete Forum and Search --> : Connect to a database in a web service!


fuffens
June 10th, 2002, 07:54 AM
Hi!

I'm trying to use a web service to connect to a database, read some data and return it. I used the following code in a console application to connect to the database:

SqlConnection thisConnection = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=northwind");
thisConnection.Open();

It works fine and I can read and present my data. But if I transfer the code to a web service, the connection causes an exception! I get the following exception message:

System.Data.SqlClient.SqlException: Login failed for user 'S0068\ASPNET'. at System.Data.SqlClient.SqlConnection.Open() at DatabaseLibrary.DataQuery.GetCompanyName() in c:\dotnet\test2\databaselibrary\dataquery.cs:line 2

Does the connection from the web service require some other login than the console application? What should I do about it?

Thanks,
Fredrik

V. Lorenzo
June 11th, 2002, 05:29 AM
Hi:

For accessing a database from a web service or ASP.NET code, there is a globals.config (or Web.config, I don't recall the exact name now) file sets a default connection string (among other things), so it may set the connection.ConnectionString to default values when not provided.

In the connection string you're providing the "User ID" and "Password" connection properties are missing. All you have to do is set them properly, like in:

"server=localhost;Database=MyDataBase;uid=MyLoggin;pwd=MyPassword"

VictorL