CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2001
    Posts
    2

    Connect to a database in a web service!

    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

  2. #2
    Join Date
    Feb 2002
    Location
    Spain
    Posts
    148
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured