0

我创建了需要从 SQL Server 获取数据的 Windows 应用程序。为了做到这一点,我还有 XML Web Service,这是我用来返回 DataSet 的代码:

[WebMethod]
public DataSet GetDataSet(SqlCommand cmd)
{
    using (SqlConnection Conn = new SqlConnection(ConnString))
    {
        cmd.Connection = Conn;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        try
        {
            //Conn.Open();
            using (DataSet DS = new DataSet())
            {
                da.Fill(DS);
                return DS;
            }
        }
        catch (Exception ex)
        {
            return (new DataSet());
        }
        finally
        {
            if (da != null)
            {
                da.Dispose();
            }
        }
    }
}

当我想将我的 Web 服务添加到我的 Win 应用程序项目中时,它会出现以下错误:

无法序列化 System.ComponentModel.Component.Site 类型的 System.ComponentModel.ISite 成员,因为它是一个接口

4

2 回答 2

1

您可能想阅读以下文章,因为我认为您不能序列化接口对象:-

http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/bac96f79-82cd-4fef-a748-2a85370a8510/

于 2012-11-15T13:23:36.263 回答
1

SqlCommand 无法序列化,因此您需要在函数中实例化。

 [WebMethod]
 public DataSet GetDataSet()
 {
 SqlCommand cmd = new SqlCommand();
 using (SqlConnection Conn = new SqlConnection(ConnString))
 {
    cmd.Connection = Conn;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    try
    {
        //Conn.Open();
        using (DataSet DS = new DataSet())
        {
            da.Fill(DS);
            return DS;
        }
    }
    catch (Exception ex)
    {
        return (new DataSet());
    }
    finally
    {
        if (da != null)
        {
            da.Dispose();
        }
    }
  }
}
于 2012-11-15T13:38:31.283 回答