1、Web服务代码
1: using System;
2: using System.Data;
3: using System.Data.SqlClient;
4: using System.Diagnostics;
5: using System.Web.Services;
6: using System.Web.Configuration;
7:
8: /// <summary>
9: /// Summary description for CategoriesService
10: /// </summary>
11: [WebService(Namespace = "http://tempuri.org/")]
12: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13: // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
14: // [System.Web.Script.Services.ScriptService]
15: public class CategoriesService : System.Web.Services.WebService {
16:
17: public CategoriesService () {
18:
19: //Uncomment the following line if using designed components
20: //InitializeComponent();
21: }
22:
23: [WebMethod]
24: public DataSet GetCategoriesAsDataset()
25: {
26: try
27: {
28: using (SqlConnection conn = new SqlConnection())
29: {
30: string connectionString = WebConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
31: conn.ConnectionString = connectionString;
32: SqlCommand command = new SqlCommand("select * from Production.ProductCategory", conn);
33: command.CommandType = CommandType.Text;
34: SqlDataAdapter adapter = new SqlDataAdapter(command);
35: DataSet categories = new DataSet("Categories");
36: adapter.Fill(categories);
37: return categories;
38: }
39: }
40: catch(Exception ex)
41: {
42: EventLog.WriteEntry("Application", ex.Message);
43: throw ex;
44: }
45:
46: }
47:
48: }
49:
2、代码结构