2012-11-15 51 views
0

我已经创建了需要从SQL Server获取数据的Windows应用程序。为了做到这一点很容易我也有XML Web Service的,这里是我用来返回DataSet中的代码:从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服务在我的Windows应用程序项目它提供了以下错误:

Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface

任何建议将高度赞赏。

在此先感谢。

回答

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(); 
     } 
    } 
    } 
} 
+0

那么这是什么答案?与有问题的代码有什么不同?我明白你的观点,但需要一点点解释。 –

+0

SqlCommand不能被序列化。因此你不能发送参数。 –

+0

然后将它写入您的答案..... –