服务器无法连接到SQL Server

时间:2010-10-04 14:26:11

标签: c# web-services

我有一个C#服务项目,我连接到SQL并检索数据。 当我在本地调试时,例如:在Visual Studion Developement Server中,它运行良好。 但是当我上传到服务器(只是localhost / MyProject /)时,SqlCommand()会抛出异常。有没有办法获得有关SqlCommand()的更多信息? 我应该设置什么权限才能在服务器上运行Web服务?

也许模式细节: VS2008 envirenoment中的项目非常好用:

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

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

例外情况并非如此:

SqlDataReader reader在第二种情况下不会返回任何结果:

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

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

                }

谢谢阿曼。

修改 仅供参考: 在一种情况下,代码通过ASP.NET Developement Server调试,第二个代码在IIS 7.0上运行

更新

经过深入挖掘后,我发现:连接是打开并连接的,通常的查询是可以的,但是存储函数的查询失​​败了......可能是IIS错过了配置吗?

3 个答案:

答案 0 :(得分:1)

如果您正在使用SqlCommand控件(拖放到页面上)而不是SqlCommand对象(代码),那么最好的办法是添加页面级错误处理程序(http://msdn.microsoft.com /en-us/library/ed577840.aspx)。

最有可能的问题是,您的连接字符串使用SSPI对SQL Server进行身份验证(集成/域安全性)。这将允许您通过Visual Studio进行连接,但不能在部署后进行连接。您可能希望查看这篇文章(http://msdn.microsoft.com/en-us/library/bsz5788z.aspx)。那篇文章的答案并不理想,但它们会让你继续前进。更好的方法会变得非常复杂。一旦您的应用再次运行,请阅读这些内容。

答案 1 :(得分:0)

您可以将VS中的调试器附加到盒子上的IIS中并继续调试。为此,请转到Debug-> Attach to Process,然后找到运行应用程序运行的应用程序池的W3wp.exe进程。

答案 2 :(得分:0)

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);
}

catch中的断点以查看调试器中的异常。

相关问题