Web服务与数据集

本例创建一个Web服务,它将SQL server服务器中的Northwind数据库中的Products表暴露给用户,通过GetProducts()方法返回一个数据集供客户端使用。
(1)创建一个新的Web服务,取名为Products。
(2)打开Products.cs。要使用SQL Server提供的系统服务,需要在文件头引用System.Data 和 System.Data.SqlClient命名空间,代码如下
using System.Data;
using System.Data.SqlSever;
(3)在Products类中添加GetProducts方法,其代码如下
[WebMethod(Description="返回产品列表")]
public Dataset GetProducts()
{
SqlConnection conn;
SqlDataAdapter myDataAdapter;
DataSet myDataSet = new DataSet();
//打开数据库连接
conn=new Sqlconnection("Data Source=(local);Initial Catalog=NorthWind;Integrated Security=True");
string cmd="Select * From Products";
MyDataAdapter=new SqlDataAdapter(cmd,conn);
//填充myDataSet,并将其返回
myDataAdapter.Fill(myDataSet,"Products");
return myDataSet;
}
(4)创建新网站并添加Web引用。
(5)打开Default.aspx,添加一个Button控件和一个GridView控件。
(6)添加Button按钮的消息响应函数
protected void Button1_Click(object sender, EventArgs e)
{
//实例化procduct代理对象
localhost.Products ws=new localhost.Products();
GridView1.DataSource=ws.GerProducts();
GridView1.DataBind();
}
(7)保存并运行网站
阅读更多

关于命名空间(重复);关于WEB服务;关于数据集

10-11

我写了一个WEB服务类。在IIS下用浏览器查看时,总是提示命名空间重复。rn在这个服务中,我引用的有重写的Dataset的类。我找不出原因(看不懂堆栈信息)。rn恳请大侠!高手相助!rn命名空间 重复。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.InvalidOperationException: 命名空间 重复。rnrn源错误: rnrn执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 rnrn堆栈跟踪: rnrnrn[InvalidOperationException: 命名空间 重复。]rn System.Xml.Serialization.XmlSchemaExporter.ExportSpecialMapping(SpecialMapping mapping, String ns, Boolean isAny)rn System.Xml.Serialization.XmlSchemaExporter.ExportElementMapping(XmlSchemaElement element, Mapping mapping, String ns, Boolean isAny) +277rn System.Xml.Serialization.XmlSchemaExporter.ExportElementAccessor(XmlSchemaGroupBase group, ElementAccessor accessor, Boolean repeats, Boolean valueTypeOptional, String ns) +921rn System.Xml.Serialization.XmlSchemaExporter.ExportElementAccessors(XmlSchemaGroupBase group, ElementAccessor[] accessors, Boolean repeats, Boolean valueTypeOptional, String ns) +100rn System.Xml.Serialization.XmlSchemaExporter.ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, String name, String ns, Boolean hasSimpleContent) +252rn System.Xml.Serialization.XmlSchemaExporter.ExportMembersMapping(MembersMapping mapping, String ns) +48rn System.Xml.Serialization.XmlSchemaExporter.ExportElementMapping(XmlSchemaElement element, Mapping mapping, String ns, Boolean isAny) +222rn System.Xml.Serialization.XmlSchemaExporter.ExportElement(ElementAccessor accessor) +225rn System.Xml.Serialization.XmlSchemaExporter.ExportMembersMapping(XmlMembersMapping xmlMembersMapping) +70rn System.Web.Services.Description.SoapProtocolReflector.CreateLiteralMessage(Message message, MessageBinding messageBinding, XmlMembersMapping members, Boolean wrapped) +395rn System.Web.Services.Description.SoapProtocolReflector.CreateMessage(Boolean rpc, SoapBindingUse use, SoapParameterStyle paramStyle, Message message, MessageBinding messageBinding, XmlMembersMapping members) +78rn System.Web.Services.Description.SoapProtocolReflector.ReflectMethod() +449rn System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding) +1818rn System.Web.Services.Description.ProtocolReflector.Reflect() +506rn System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[] reflectors) +495rn System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type type, String url) +112rn System.Web.Services.Protocols.DocumentationServerType..ctor(Type type, String uri) +158rn System.Web.Services.Protocols.DocumentationServerProtocol.Initialize() +269rn System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +106rnrn[InvalidOperationException: 无法处理请求。]rn System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +205rn System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +82rnrn[InvalidOperationException: 未能处理请求。]rn System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +154rn System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +94rn System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, String path, String pathTranslated, Boolean useAppConfig) +696rn System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +95rn System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +173rnrn rn

没有更多推荐了,返回首页