发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
创建和使用 XML Web 服务。小案例实现 Web 服务的创建、发布和调用。
using System; using System.Collections.Generic; using System.Web; using System.Web.Services; using System.Data.SqlClient; using System.Data; /// <summary> ///WebService 的摘要说明 /// </summary> [WebService(Namespace = "http:///",Name="ylb科技",Description="专业的WebService服务")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } /// <summary> /// 带方法名注释 MessageName="欢迎某人" /// </summary> /// <returns></returns> [WebMethod(MessageName="欢迎某人")] public string HelloWorld2() { return "欢迎你伟大的ylb先生。"; } /// <summary> /// 返回所有产品信息 /// 用适配器做的,无参数 /// </summary> /// <returns></returns> [WebMethod] public DataTable GetProducts() { string sql = "select * from Products"; DataSet ds = new DataSet(); DataTable dt = new DataTable(); SqlConnection con = new SqlConnection(); con.ConnectionString = "server=.;database=northwind;integrated security=sspi"; SqlDataAdapter adapter = new SqlDataAdapter(sql, con); con.Open(); adapter.Fill(ds); con.Close(); ///取出表 dt = ds.Tables[0]; return dt; } /// <summary> /// 返回产品集合,根据CategoryID /// 有参数 /// </summary> /// <param name="cateID"></param> /// <returns></returns> [WebMethod(CacheDuration = 600)] //设置缓存以秒为单位 public DataTable GetProductsByCateID(int cateID) { string sql = "select * from Products where categoryID=@categoryID"; DataSet ds = new DataSet(); DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(); conn.ConnectionString = "server=.;database=northwind;integrated security=sspi"; SqlCommand com = conn.CreateCommand(); com.CommandText = sql; //配参 com.Parameters.Add(new SqlParameter("@categoryID", cateID)); conn.Open(); SqlDataReader sdr = com.ExecuteReader(); dt.Load(sdr); conn.Close(); ds.Tables.Add(dt); dt = ds.Tables[0]; return dt; } }
<configuration> <appSettings> <add key="localhost.WebService" value="http://localhost:50864/WebSite/WebService.asmx"/> </appSettings> </configuration>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoHouTai.aspx.cs" Inherits="DemoHouTai" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h3> 后台调用WebService应用</h3> <h2> 方法:添加对象 </h2> <hr /> <h4> 案例:调用查所有产品信息,无参数</h4> <asp:GridView ID="gvwProductList" runat="server" AllowPaging="True" EnableModelValidation="True" onpageindexchanging="gvwProductList_PageIndexChanging"> </asp:GridView> </div> </form> </body> </html>
using System; using System.Web.UI.WebControls; public partial class DemoHouTai : System.Web.UI.Page { /// <summary> /// 展示所有产品 /// </summary> private void BindProductList() { gvwProductList.DataSource = new localhost.ylb科技().GetProducts(); gvwProductList.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //调用 BindProductList(); } } protected void gvwProductList_PageIndexChanging(object sender, GridViewPageEventArgs e) { //新页码 gvwProductList.PageIndex = e.NewPageIndex; //更新数据 BindProductList(); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoHouTai2.aspx.cs" Inherits="DemoHouTai2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h3> 后台调用WebService应用</h3> <h2> 方法:添加对象 </h2> <hr /> <h4> 案例:调用查所有产品信息,有参数</h4> <hr /> 请输入产品类别:<asp:TextBox ID="txtCategoryID" runat="server"></asp:TextBox> <asp:Button ID="btnSubmit" runat="server" Text="搜索" onclick="btnSubmit_Click" /> <div> <asp:Label ID="lblState" runat="server" ForeColor="Red" ></asp:Label></div> <hr /> 该分类下的产品是:<br /> <asp:GridView ID="gvwProductList" runat="server" AllowPaging="True" EnableModelValidation="True" onpageindexchanging="gvwProductList_PageIndexChanging"> </asp:GridView> </div> </form> </body> </html>
using System; using System.Web.UI.WebControls; public partial class DemoHouTai2 : System.Web.UI.Page { /// <summary> /// 绑定产品类表,根据CategoryID /// </summary> private void BindProductList() { lblState.Text = string.Empty; int categoryID= 1; if (IsPostBack) //不是第一次加载该页面 { if (txtCategoryID.Text.Length > 0) { categoryID= Convert.ToInt32(txtCategoryID.Text.Trim()); } } gvwProductList.DataSource = new localhost.ylb科技().GetProductsByCateID(categoryID); gvwProductList.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //调用 BindProductList(); } } protected void btnSubmit_Click(object sender, EventArgs e) { BindProductList(); } protected void gvwProductList_PageIndexChanging(object sender, GridViewPageEventArgs e) { //当前页码 gvwProductList.PageIndex = e.NewPageIndex; //更新数据 BindProductList(); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoQianTai.aspx.cs" Inherits="DemoQianTai" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h3>前台调用WebService应用</h3> <h2> 方法:添加对象 </h2> <hr /> <h4>案例:调用查所有产品信息,无参数</h4> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1" EnableModelValidation="True"> </asp:GridView> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProducts" TypeName="localhost.ylb科技"> </asp:ObjectDataSource> </div> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoQianTai2.aspx.cs" Inherits="DemoQianTai2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h3> 前台调用WebService应用</h3> <h2> 方法:添加对象 </h2> <hr /> <h4> 案例:调用查所有产品信息,有参数</h4> <hr /> 请输入产品类别:<asp:TextBox ID="txtCategoryID" runat="server"></asp:TextBox> <asp:Button ID="btnSubmit" runat="server" Text="搜索" /> <hr /> 该分类下的产品是:<br /> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1" EnableModelValidation="True"> </asp:GridView> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProductsByCateID" TypeName="localhost.ylb科技"> <SelectParameters> <asp:ControlParameter ControlID="txtCategoryID" DefaultValue="1" Name="cateID" PropertyName="Text" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> </div> </form> </body> </html>
http://files.cnblogs.com/ylbtech/XmlWeb-WebService-WebForm-PromulgatorAndCaller.rar
来自: 昵称10504424 > 《C#》
0条评论
发表
请遵守用户 评论公约
GridView动态添加行
19 }20 }21 22 private DataTable ReadGridView()23 {24 DataTable dt = new DataTable();Add(new DataColumn("ProductID", typeof(string)));GridView1.i++)30 {31 dr = dt.44 DataRow ...
.NET中的缓存知识总结
NET中的缓存知识总结。页面部分缓存的实现包括两种方式:控件缓存和替换后缓存。前者也可称为片段缓存,这种方式允许将需要缓存的信息包含在一个用户控件内,然后,将该用户控件标记为可缓存的,以此来...
对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成
然后就是最后的更新,当然是用DataAdapter的Update()方法借助CommandBuilder来实现,要注意的是,如果你的数据库一开始没有定义主键,那进行更新的时候会出错,返回的错误将是“对于不返回任何键列信息...
C#开发和调用Web Service
C#开发和调用Web Service1.1、Web Service基本概念。是XML Web Service 的通信协议。二、Web Service开发。运行可以看到我们自己写的可...
asp.net 2.0 + sqlserver2005 数据依赖缓存 - 钱途无梁 - 博...
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/x...
webserive
上一篇我们讲了ASP.NET网络应用程序调用webservice方式 这篇我们讲采用Javascript的方式调用 回顾上一篇内容 DOTNET下的三种调用webservice方式之ASP.NET调用方式 新建一个ASP.NET网络应用程序,我取...
FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一)
[html] <asp:SqlDataSource ID="sdsCategory" runat="server" ConnectionString="<%$ ConnectionString...
ASP.NET购物车(源码下载)
ASP.NET2.0中Gridview中数据操作技巧
ASP.NET2.0中Gridview中数据操作技巧中新增的gridview控件,是十分强大的数据展示控件,在前面的系列文章里,分别展示了其中很多的基本用法和技巧(详见天极<<ASP.NET 2.0中Gridview控件高级技巧>>...
微信扫码,在手机上查看选中内容