>我创建了需要从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.ISite 类型的成员 System.ComponentModel.Component.Site,因为它是一个接口
您可能想阅读以下文章,因为我认为您无法序列化接口对象:-
http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/bac96f79-82cd-4fef-a748-2a85370a8510/
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();
}
}
}
}