一 Web Service生成
 1.新建一个ASP.NET网站 (我用的是Visual Studio2008)
a.打开Visual Studio2008,点击文件—>新建—>网站
b.选择ASP.NET网站
2.在解决方案资源管理器下找到刚新建的网站,选中右键,选择“添加新项”
3.选择“Web服务”
4.打开WebService.cs,如下图,可以在改文件中进行逻辑代码的编写,[WebMethod]是声明一个web服务方法,可以供服务端调用(相当于暴露给客户端),若不想被客户端暴露则不用写上[WebMethod]。
// 以如下代码 实现从数据库学生表中根据学号读取学生信息和更新学生信息功能为例
//WebService.cs文件
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Services;
 using System.Data.SqlClient;
 using System.Data;
  
 /// <summary>
 ///WebService 的摘要说明
 /// </summary>
 [WebService(Namespace = "http://student.org/")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
 // [System.Web.Script.Services.ScriptService]
 public class WebService : System.Web.Services.WebService {
  
     public WebService () {
         //如果使用设计的组件,请取消注释以下行 
         //InitializeComponent(); 
     }
     
     private SqlCommand cmd=null;
     private SqlConnection conn=null;
     private String sql=null;
     ///创建数据库连接方法封装
     public void openDatabase() {
         conn = new SqlConnection();
         conn.ConnectionString ==System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
         if (conn.State == ConnectionState.Closed)
         {
             conn.Open();
             //Response.Write("<script>alert('Connected!');</script>");
         }
     }
   
   ///根据学号查询学生信息
   ///web service 的返回值必须是可序列化的,而qlDataReader类型不能被序列化会出错,这里用DataSet类型
      [WebMethod]
     public DataSet SelStudent(String xuehao)
     {
             openDatabase();
             String sql = "select * from S where 学号='" + xuehao + "'";
             SqlDataAdapter sqlad = new SqlDataAdapter(sql, conn);//创建SqlDataAdapter对象并执行sql查询
             DataSet ds = new DataSet();
             sqlad.Fill(ds);
             if (conn.State != ConnectionState.Closed)
             {
                 conn.Close();
             }
             return ds;
     }
     
      //更新修改学生信息
     //数据库中身高、体重为float类型,若height、weight为“”空会出错,将其赋值null
      [WebMethod]
      public Boolean UpdateStudent(String xuehao, String height, String weight,String used_name,String email)
      {
          String strError = "";
          String sql = "update S set ";
          
          if (height.Trim() == "") { 
              height = null;
          }
          else { 
              float.Parse(height);
              sql=sql+"身高="+height+",";
          }
          if (weight.Trim() == "")
          {
              weight = null;
          }
          else
          {
              float.Parse(weight);
              sql=sql+ "体重=" + weight + ",";
          }
          if (used_name.Trim() != "") sql = sql + "曾用名='" + used_name + "',";//曾用名为数据库字段名
          if (email.Trim() != "") sql = sql + "电子邮件='" + email+ "',";
          sql = sql.TrimEnd(',') + " where 学生学号='" + xuehao + "'"; 
          try
          {
              openDatabase();
              cmd = new SqlCommand(sql,conn);
              int count = cmd.ExecuteNonQuery();
              return !(count<1);
          }
          catch(Exception e){
              strError = "更新数据库失败:" +e.Message;
              return false;
          }
          finally
          {
              if (conn.State != ConnectionState.Closed)
              {
                  conn.Close();
              }
          }
      }
     
 }
 5.启动运行项目,在浏览器中可以查看测试调用页面,可以看到SelStudent、UpdateStudent方法可以被调用
6.点击SelStudent进入,输入xuehao(学号)可以进行测试调用
二 Web Service客户端调用
1.用IIS发布我们刚刚生成的Web Service服务
a.打开IIS,找到“网站”,右键“添加网站”,物理路径选择之前建的项目所在路径,我这里是WebService2所在路径,端口要填写未被占用的。添加完毕可以在浏览器中输入 示例(我的):http://127.0.0.1:53414/WebService.asmx进行查看
2.新建ASP.NET 网站,在解决方案资源管理器下找到新建项目名,右键选择“添加服务引用”
3. 地址:主机地址:端口号/+.asm文件,端口号即IIS发布设定的端口,点击前往,会显示服务(我的是显示WebService)
4.如下图会生成ServiceReference1
5.在Default.aspx.cs中编写调用
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Data;
 using System.Data.SqlClient;
  
 public partial class _Default : System.Web.UI.Page
 {
     protected void Page_Load(object sender, EventArgs e)
     {
         ServiceReference1.WebServiceSoapClient ws = new ServiceReference1.WebServiceSoapClient();
         DataSet ds=ws.SelStudent("201344444");
        Label1.Text = ds.Tables[0].Rows[0][0].ToString();
     }
 }
 6.运行项目在浏览器查看调用成功'
另外:服务方法不能用static修饰。
来源:oschina
链接:https://my.oschina.net/8824/blog/3195984
