【问题标题】:Can't execute SQL in WCF无法在 WCF 中执行 SQL
【发布时间】:2015-05-07 16:24:48
【问题描述】:

我尝试在 WCF 服务中收集数据,但总是出错

由于内部错误,服务器无法处理请求。有关该错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为)以便将异常信息发送回客户端,或者根据 Microsoft .NET Framework SDK 文档打开跟踪并检查服务器跟踪日志。

当我卸载 SQL Server 2005 并将其替换为 SQL Server 2008 时会发生这种情况。请有人帮助我

这是我的代码

public DataTable fillData(string connection, string tableName)
{
    DataTable dt = new DataTable();

    using (SqlConnection con = new SqlConnection())
    {
        con.Open();

        using (SqlDataAdapter da = new SqlDataAdapter("select * from " + tableName, con)) 
        {
            da.Fill(dt);
        }
    }

    return dt;
}

【问题讨论】:

    标签: c# wcf sql-server-2008


    【解决方案1】:

    您没有提供连接对象和打开连接对象的任何详细信息。因此,您可能会遇到异常。使用其他版本的SqlConnection 构造函数SqlConnection(string) 并在SqlConnection 的构造函数中传递您在参数中的连接字符串。

    作为附加说明,请使用try catch 处理异常

    public DataTable fillData(string connection, string tableName)
    {
        DataTable dt = new DataTable();
        try
        {
           using (SqlConnection con = new SqlConnection(connection))
           {
               con.Open();
               using (SqlDataAdapter da = new SqlDataAdapter("select * from " + tableName,con))
               {
                    da.Fill(dt);
               }
           }
         }
         catch(Exception ex)
         {
             //You may log exception here and show some message to user
         }
        return dt;
    }
    

    就您收到的消息而言,表明 WCF 服务设置不允许显示内部服务器错误的详细信息。您可以在开发过程中允许这样做,以使用 ServiceBehaviorAttribute.IncludeExceptionDetailInFaults 属性获取确切的错误消息。

    ServiceBehaviorAttribute.IncludeExceptionDetailInFaults

    获取或设置一个值,该值指定一般未处理的执行 异常将被转换为 System.ServiceModel.FaultException 类型 System.ServiceModel.ExceptionDetail 并作为故障消息发送。放 这仅在开发过程中为 true,用于对服务进行故障排除

    【讨论】:

    • 我没有看到,真的很抱歉。我添加了连接参数并弹出错误“底层连接已关闭:连接已意外关闭。”
    猜你喜欢
    • 2016-01-09
    • 2020-01-25
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多