如何在wcf中返回数据集 [英] How to return dataset in wcf
本文介绍了如何在wcf中返回数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ServiceModel;
namespace WCFTemplateExample
{
[ServiceContract]
interface WCFInterface
{
[OperationContract]
DataSet GetData();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace WCFTemplateExample
{
public class WCFClass :WCFInterface
{
#region WCFInterface Members
public System.Data.DataSet GetData()
{
SqlConnection con = new SqlConnection(@"Data Source=iicss20\PMIS;Initial Catalog=MSSLMS;Persist Security Info=True;User ID=nkhn3059;Password=nkhn3059");
SqlDataAdapter da = new SqlDataAdapter("select * from ss.lease",con);
DataSet ds = new DataSet();
da.Fill(ds, "ss.lease");
return ds;
}
#endregion
}
}
<system.serviceModel>
<services>
<service name="WCFTemplateExample.WCFClass" behaviorConfiguration="WCFTemplateExample.WCFClassBehavior">
<host>
<baseAddresses>
<add baseAddress = "net.tcp://localhost:8731/Design_Time_Addresses/WCFTemplateExample/WCFClass/" />
</baseAddresses>
</host>
<endpoint address="net.tcp://localhost:8008/WCFClass" binding="netTcpBinding" contract="WCFTemplateExample.WCFInterface"></endpoint>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<!--<endpoint address ="" binding="Binding" contract="WCFTemplateExample.WCFInterface">
--><!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
--><!--
<identity>
<dns value="localhost"/>
</identity>
</endpoint>-->
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFTemplateExample.WCFClassBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="false"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
它不返回包含DataSet的Service方法..
It's not return the Service method which contains DataSet..
推荐答案
不要在WCF服务中使用数据集。使用集合或数据类。
检查这一点以了解为什么我们不应该使用数据集作为WCF服务合同中的返回类型。
http://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx [ ^ ]
如果您仍想使用,请查看这些链接
1. http://www.silverlighthostingnews.com/index.php/archives/461 [ ^ ]
2. http://jwalin.wordpress.com/2011/03/16/passing-large-dataset-to- webwcf-service / [ ^ ]
Dont use dataset in WCF services. Use Collection or dataclasses.
Check this to know why we shouldnt use Dataset as return type in WCF service contract.
http://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx[^]
If you still want to use, then check these links
1. http://www.silverlighthostingnews.com/index.php/archives/461[^]
2. http://jwalin.wordpress.com/2011/03/16/passing-large-dataset-to-webwcf-service/[^]
SqlConnection con = new SqlConnection(@Data Source = iicss20 \ PMIS; Initial Catalog = MSSLMS; Persist Security Info = True; User ID = nkhn3059;密码= nkhn3059);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.TableMappings.Add(Table ,lease);
con.Open();
SqlCommand command = new SqlCommand(
SELECT * FROM ss.lease ;,
连接);
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.Fill(dataSet);
con.Close();
SqlConnection con = new SqlConnection(@"Data Source=iicss20\PMIS;Initial Catalog=MSSLMS;Persist Security Info=True;User ID=nkhn3059;Password=nkhn3059");
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.TableMappings.Add("Table", "lease");
con.Open();
SqlCommand command = new SqlCommand(
"SELECT * FROM ss.lease;",
connection);
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.Fill(dataSet);
con.Close();
这篇关于如何在wcf中返回数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文