本文介绍了无法使用webservice显示数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好我正在尝试使用网络服务从数据库检索数据但它无法正常工作 我有3个文件:ProductService.cs,ProductServiceClient.aspx,ProductService.svc 以下是我正在使用的代码: ProductService.cs文件: Hi I am trying to retrieve data from db using a webservice but it is not workingI got 3 files:ProductService.cs,ProductServiceClient.aspx,ProductService.svcHere is the code I am using :ProductService.cs file:public class ProductService{ [OperationContract] public string GetProductDetailsByProductID(int productID) { Product prod = new Product(); string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ToString(); using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = "Select Name, ProductNumber from dbo.Product Where ProductID = " + productID.ToString(); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { prod.Name = reader["Name"].ToString(); prod.ProductNumber = reader["ProductNumber"].ToString(); prod.ProductID = productID; } } MemoryStream stream = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Product)); serializer.WriteObject(stream, prod); stream.Position = 0; StreamReader streamReader = new StreamReader(stream); return streamReader.ReadToEnd(); }}[DataContract]public class Product{ [DataMember] public int ProductID; [DataMember] public string Name; [DataMember] public string ProductNumber;} 这就是我如何打电话或推荐网络服务 And this how I am calling or refernce the webservice <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript"> function pageLoad() { } function OnGetProductDetailsByProductID() { ProductService.GetProductDetailsByProductID($get("txtProductID").value, OnGetProductDetailsByProductIDComplete, OnError); } function OnGetProductDetailsByProductIDComplete(result) { var prod = eval("(" + result + ")"); $("spnProductID").innerText = prod.ProductID; $("spnProductName").innerText = prod.Name; $("spnProductNumber").innerText = prod.ProductNumber; } function OnError(errorMessage) { alert(errorMessage.get_message()); } </script></head><body> <form id="form1" runat="server"> <div> Enter Product ID: <input type="text" id="txtProductID" name="txtProductID" /> <input type="button" value="Get Product Details" id="btnInvokeWebService" onclick="OnGetProductDetailsByProductID()" /> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/ProductService.svc" /> </Services> </asp:ScriptManager> <br /><br /> Product ID : <span id="spnProductID"></span> <br /><br /> Name :<span id="spnProductName"></span> <br /><br /> Product Number :<span id="spnProductNumber"></span> <br /><br /> </div> </form> 和最后一个文件i s ProductService.svc: and the last file is ProductService.svc:<%@ ServiceHost Language="C#" Debug="true" Service="ProductService" CodeBehind="~/App_Code/ProductService.cs" %> 我不知道为什么数据是n显示。 请告诉我原因,请有人帮忙吗? 我可以在firebug的控制台面板中看到响应中的数据但是没有显示在浏览器。I dont know why the data are not displayed.Please can someone help by telling me why?by the way I can see the data in the response in console panel of firebug but are not displayed in the browser.推荐答案 这篇关于无法使用webservice显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-18 18:35