简体   繁体   中英

Server cannot connect to SQL server

I have a C# Service project where I am connectiong to SQL and retrieving data. When I debug locally eg: in the Visual Studion Developement Server it works nice. But when I upload to the server(simply localhost/MyProject/) The SqlCommand() is throws an exception. Are there way to get more information on SqlCommand()? What permissions I should set to run web service on the server?

Maybe mode details: The project within VS2008 envirenoment is works nice:

http://localhost:50301/GetJpeg.aspx?ra=224.5941&dec=-1.09&width=1000&height=1000&scale=1

but on the http://localhost/GetJpeg.aspx?ra=224.5941&dec=-1.09&width=1000&height=1000&scale=1 no.

The exception is not really exception the:

SqlDataReader reader does not return any result in second case:

reader = cmdCenter.ExecuteReader();
                if (reader.Read()) 
                {
                    //Do Something

                    reader.Close(); 
                }                               
                else 
                {                   
                    throw new Exception("Request is failed");

                }

Thanks Arman.

EDIT Just for information: In one case the code is debugged via ASP.NET Developement Server and the second one is running on IIS 7.0

UPDATE

After deep digging I discovered: The connection is open and connected, usual queries is ok, but queries with stored functions are failing... can be that IIS miss configuration ?

If you are using the SqlCommand control (drag-dropped onto a page) instead of the SqlCommand object (code), your best bet will be to add a page-level error handler (http://msdn.microsoft.com/en-us/library/ed577840.aspx).

Most likely, the problem is that your connection string uses SSPI for authenticating to the SQL Server (integrated/domain security). That would allow you to connect via Visual Studio, but not once it is deployed. You might want to look at this article (http://msdn.microsoft.com/en-us/library/bsz5788z.aspx). The answers in that article are not ideal, but they will get you moving along. The better approaches can get pretty complicated. Read-up on those once you get your app working again.

You can attach the Debugger in VS to IIS on your box and continue debugging. To do this, go to Debug->Attach to Process and then find the W3wp.exe process that is running the Application Pool your application is running in.

try
{
    using (SqlConnection connection = new SqlConnection("Your connection string here"))
    {
        using (SqlCommand command = new SqlCommand("your sql here", connection))
        {
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
            }
        }
    }
}
catch (Exception exception)
{
    System.Diagnostics.Debug.WriteLine(exception);
}

Breakpoint in the catch to see the exception in your debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM