employeeservice.asmx

来自「《圣殿祭司的ASP.NET 2.0开发详解——使用C#》光盘内容.包含了书籍所含」· ASMX 代码 · 共 56 行

ASMX
56
字号
<%@ WebService Language="C#" Class="Samples.AspNet.EmployeeService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;

namespace Samples.AspNet {

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class EmployeeService : System.Web.Services.WebService
    {
        [WebMethod]
        public string Employee(String txtFirstName)
        {
            SqlConnection conn = new SqlConnection("data source=.;initial catalog=Northwind;user id=sa;password=test");
            conn.Open();

            SqlCommand cmd = new SqlCommand("select EmployeeID,FirstName,City,Address from Employees where FirstName=@FirstName", conn);
            cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = txtFirstName;
            SqlDataReader dr = cmd.ExecuteReader();

            string txtMsg = "";

            if (dr.Read())
            {
                txtMsg = "员工代表:" + dr["EmployeeID"] + "  ,";
                txtMsg += "姓名:" + dr["FirstName"] + "  ,";
                txtMsg += "居住城市:" + dr["City"] + "  ,";
                txtMsg += "地址:" + dr["Address"] + "  ,";
            }
            else
            {
                if (String.IsNullOrEmpty(txtFirstName))
                {
                    txtMsg = "<Font Color='Blue'>请输入姓名</Font>";
                }
                else
                {
                    txtMsg = "<Font Color='Red'>查无此人!</Font>";
                }
            }

            cmd.Dispose();
            dr.Dispose();
            conn.Dispose();


            return txtMsg;  //返回用户详细信息
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?