C# 从XML Web服务返回数据集时出错

C# 从XML Web服务返回数据集时出错,c#,web-services,C#,Web Services,我创建了需要从SQL Server获取数据的Windows应用程序。为了方便起见,我还提供了XML Web服务,下面是用于返回数据集的代码: [WebMethod] public DataSet GetDataSet(SqlCommand cmd) { using (SqlConnection Conn = new SqlConnection(ConnString)) { cmd.Connection = Conn; SqlDataAdapter

我创建了需要从SQL Server获取数据的Windows应用程序。为了方便起见,我还提供了XML Web服务,下面是用于返回数据集的代码:

[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.Component.Site,因为它是接口


您可能需要阅读以下文章,因为我认为您无法序列化接口对象:-


无法序列化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();
        }
    }
  }
}

那么这个答案是什么呢?与所讨论的代码有什么不同?我明白你的意思,但需要一点解释。SqlCommand无法序列化。因此您不能发送参数。@L.B谢谢您的回复。但我之所以想发送参数,是因为我调用了4或5个SQL命令,我想使用ready DataSet轻松地发送带有参数的CommandText并获取表。因为我不想每次都创建连接。因为我想在我所有的项目表单中使用这个数据集,我需要连接到数据库来获取数据。@Shohin的答案来自Muhammad Hussain,而不是我。