简体   繁体   English

服务器无法连接到SQL Server

[英]Server cannot connect to SQL server

I have a C# Service project where I am connectiong to SQL and retrieving data. 我有一个C#服务项目,在这里我要连接到SQL并检索数据。 When I debug locally eg: in the Visual Studion Developement Server it works nice. 当我在本地调试时,例如:在Visual Studion开发服务器中,它可以很好地工作。 But when I upload to the server(simply localhost/MyProject/) The SqlCommand() is throws an exception. 但是,当我上传到服务器时(仅是localhost / MyProject /),SqlCommand()会引发异常。 Are there way to get more information on SqlCommand()? 有没有办法获得有关SqlCommand()的更多信息? What permissions I should set to run web service on the server? 我应该设置什么权限才能在服务器上运行Web服务?

Maybe mode details: The project within VS2008 envirenoment is works nice: 可能的模式详细信息:VS2008环境中的项目很好用:

http://localhost:50301/GetJpeg.aspx?ra=224.5941&dec=-1.09&width=1000&height=1000&scale=1 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. 但在http://localhost/GetJpeg.aspx?ra = 224.5941&dec = -1.09&width = 1000&height = 1000&scale = 1上没有。

The exception is not really exception the: 异常并不是真正的异常:

SqlDataReader reader does not return any result in second case: 在第二种情况下, SqlDataReader reader不返回任何结果:

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 编辑仅供参考:在一种情况下,代码是通过ASP.NET Developement Server调试的,第二种是在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 ? 深入研究后,我发现:连接已打开并已连接,通常的查询都可以,但是具有存储功能的查询失败了……这可能是IIS缺少配置吗?

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). 如果您使用的是SqlCommand控件(拖放到页面上)而不是SqlCommand对象(代码),则最好的选择是添加页面级错误处理程序(http://msdn.microsoft.com/zh-我们/library/ed577840.aspx)。

Most likely, the problem is that your connection string uses SSPI for authenticating to the SQL Server (integrated/domain security). 问题很可能是您的连接字符串使用SSPI对SQL Server进行身份验证(集成/域安全性)。 That would allow you to connect via Visual Studio, but not once it is deployed. 这样一来,您就可以通过Visual Studio进行连接,但是一旦部署就不能进行连接。 You might want to look at this article (http://msdn.microsoft.com/en-us/library/bsz5788z.aspx). 您可能想看一下这篇文章(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. 您可以将VS中的调试器附加到框上的IIS,然后继续进行调试。 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. 为此,请转到“调试”->“附加到进程”,然后找到正在运行应用程序池的W3wp.exe进程。

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. 捕获中的断点以查看调试器中的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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