C# コードの書き方
C# |
コードのコピー
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Orders の概要 /// </summary> public class Orders { private double amount; private string year,month,day; public string Year { get { return year; } set { year = value; } } public string Month { get { switch (month) { case "1": month = "Jan"; break; case "2": month = "Feb"; break; case "3": month = "Mar"; break; case "4": month = "Apr"; break; case "5": month = "May"; break; case "6": month = "Jun"; break; case "7": month = "Jul"; break; case "8": month = "Aug"; break; case "9": month = "Sep"; break; case "10": month = "Oct"; break; case "11": month = "Nov"; break; case "12": month = "Dec"; break; } return month; } set { month = value; } } public string Day { get { return day; } set { day = value; } } public double OrderAmount { get { return amount; } set { amount = value; } } } |
C# コードの書き方
C# |
コードのコピー
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; using System.Data.OleDb; using System.Web.Script.Services; /// <summary> /// GetOrders の概要 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // ASP.NET AJAX を使用してスクリプトからこの Web サービスを呼び出せるようにするには、次の行のコメントを外します。 [System.Web.Script.Services.ScriptService] public class GetOrders : System.Web.Services.WebService { public GetOrders () { //デザイン済みのコンポーネントを使用する場合は、次の行のコメントを外します //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<Orders> GetDataOnLoad() { OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/OrdersDataBase.mdb")); OleDbCommand cmd = new OleDbCommand("Select Year(OrderDate), Sum(OrderAmount) from OrdersByDate where Year(OrderDate) In (Select Distinct(Year(OrderDate)) from OrdersByDate) Group By Year(OrderDate)", con); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); List<Orders> orders = new List<Orders>(); for (int i = 0; i < dt.Rows.Count; i++) { Orders od = new Orders(); od.Year = Convert.ToString(dt.Rows[i][0]); od.OrderAmount = Convert.ToDouble(dt.Rows[i][1]); orders.Add(od); } return orders; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<Orders> GetOrderByMonth(string Year) { OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/OrdersDataBase.mdb")); OleDbCommand cmd = new OleDbCommand("Select Month(OrderDate), Sum(OrderAmount) from OrdersByDate where Month(OrderDate) In (Select Distinct Month(OrderDate) from OrdersByDate) and Year(OrderDate)="+ Year + " Group By Month(OrderDate)", con); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataTable dtMonths = new DataTable(); da.Fill(dtMonths); List<Orders> orders = new List<Orders>(); for (int i = 0; i < dtMonths.Rows.Count; i++) { Orders od = new Orders(); od.Month = Convert.ToString(dtMonths.Rows[i][0]); od.OrderAmount = Convert.ToDouble(dtMonths.Rows[i][1]); orders.Add(od); } return orders; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<Orders> GetOrderByDay(string Month, string Year) { switch (Month) { case "Jan": Month = "1"; break; case "Feb": Month = "2"; break; case "Mar": Month = "3"; break; case "Apr": Month = "4"; break; case "May": Month = "5"; break; case "Jun": Month = "6"; break; case "Jul": Month = "7"; break; case "Aug": Month = "8"; break; case "Sep": Month = "9"; break; case "Oct": Month = "10"; break; case "Nov": Month = "11"; break; case "Dec": Month = "12"; break; } OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/OrdersDataBase.mdb")); OleDbCommand cmd = new OleDbCommand("Select Day(OrderDate), Sum(OrderAmount) from OrdersByDate where Day(OrderDate) In (Select Distinct Day(OrderDate) from OrdersByDate) and Year(OrderDate)=" + Year + " and Month(OrderDate)="+Month+" Group By Day(OrderDate)", con); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataTable dtDays = new DataTable(); da.Fill(dtDays); List<Orders> orders = new List<Orders>(); for (int i = 0; i < dtDays.Rows.Count; i++) { Orders od = new Orders(); od.Day = Convert.ToString(dtDays.Rows[i][0]); od.OrderAmount = Convert.ToDouble(dtDays.Rows[i][1]); orders.Add(od); } return orders; } } |
ソースビュー |
コードのコピー
|
---|---|
<%@ WebService Language="C#" CodeBehind="~/App_Code/GetOrders.cs" Class="GetOrders" %> |