employeeservice.cs

来自「《ASP.NET 2.0 XML 高级编程(第3版)》 《ASP.NET 2.」· CS 代码 · 共 50 行

CS
50
字号
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class EmployeeService : System.Web.Services.WebService
{
    public EmployeeService () 
	{
    }

    [WebMethod]
    public XmlDocument GetEmpDetailsByEmpID(int employeeID)
    {
        try
        {
            string connString = WebConfigurationManager.ConnectionStrings
              ["adventureWorks"].ConnectionString;
            using (SqlConnection sqlConnection = new SqlConnection(connString))
            {
                DataSet employeeDataset = new DataSet("EmployeesRoot");
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                  ("Select EmployeeID, Title, CAST(BirthDate AS char(12)) " +
                  "AS BirthDate, MaritalStatus from HumanResources.Employee " +
                  "as Employees Where EmployeeID =" + employeeID.ToString(),
                  sqlConnection);
                command.CommandType = CommandType.Text;
                adapter.SelectCommand = command;
                //Fill the Dataset with the return value from the stored procedure
                adapter.Fill(employeeDataset, "Employees");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(employeeDataset.GetXml());
                return xmlDoc;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
}

⌨️ 快捷键说明

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