2016-05-15 15 views
0

での問い合わせ:C#の - ASP.NET - REST API - 私はここにこのチュートリアルに従うことをしようとしていますコントローラ

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

REST APIを取得するには、私が働いているウェブサイトのために設定しました。私は前にASP.NETで何も作ったことがないので、いくつかのものが私を捨てている。私は(モデルの変化に伴って)ここにそれを作った:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using DashBoardWidgets.Models; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Mvc; 

namespace DashBoardWidgets.Controllers 
{ 
    public class BoilerReadingsController : ApiController 
    { 
     public BoilerReading ExecuteQuery() 
     { 
      var MyBoilerReading = new BoilerReading(); 
      DataTable dt = new DataTable(); 
      SqlConnection connection = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=DNN;User ID=sa;Password=*****;"); 
      connection.Open(); 
      SqlCommand sqlCmd = new SqlCommand("select top 1 * from DNN.dbo.avtActionForm_BoilerReadingsLog order by TimeStamp", connection); 
      SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); 
      sqlDa.Fill(dt); 
      if (dt.Rows.Count > 0) 
      { 
       DateTime TimeStamp_q = (DateTime)dt.Rows[0]["TimeStamp"]; 
       int TurbineChosen_q = (int)dt.Rows[0]["TurbineChosen"]; 
       decimal MOPSuctionPressure_q = (decimal)dt.Rows[0]["MOPSuctionPressure"]; 
       decimal LubeOilPressure_q = (decimal)dt.Rows[0]["LubeOilPressure"]; 
       decimal ControlOilPressure_q = (decimal)dt.Rows[0]["ControlOilPressure"]; 
       decimal LubeOilTemp_q = (decimal)dt.Rows[0]["LubeOilTemp"]; 
       decimal BRNOilTemp1_q = (decimal)dt.Rows[0]["BRNOilTemp1"]; 
       decimal TBDRNOilTempFront_q = (decimal)dt.Rows[0]["TBDRNOilTempFront"]; 
       decimal TBDRNOilTempRear_q = (decimal)dt.Rows[0]["TBDRNOilTempRear"]; 
       decimal BRNOilTemp2_q = (decimal)dt.Rows[0]["BRNOilTemp2"]; 
       decimal BRNOilTemp3_q = (decimal)dt.Rows[0]["BRNOilTemp3"]; 
       decimal BRNOilTemp4_q = (decimal)dt.Rows[0]["BRNOilTemp4"]; 
       decimal BRNOilTemp5_q = (decimal)dt.Rows[0]["BRNOilTemp5"]; 
       decimal BRNOilTemp6_q = (decimal)dt.Rows[0]["BRNOilTemp6"]; 
       decimal BRNOilTemp7_q = (decimal)dt.Rows[0]["BRNOilTemp7"]; 
       decimal SealOilPressureExciteEnd_q = (decimal)dt.Rows[0]["SealOilPressureExciteEnd"]; 
       decimal SealOilPressureTurbineEnd_q = (decimal)dt.Rows[0]["SealOilPressureTurbineEnd"]; 
       string Username_q = dt.Rows[0]["Username"].ToString(); 
       int Index_q = (int)dt.Rows[0]["Index"]; 
       new BoilerReading { TimeStamp = TimeStamp_q, TurbineChosen = TurbineChosen_q, MOPSuctionPressure = MOPSuctionPressure_q, LubeOilTemp = LubeOilTemp_q, ControlOilPressure = ControlOilPressure_q, BRNOilTemp1 = BRNOilTemp1_q, TBDRNOilTempFront = TBDRNOilTempFront_q, TBDRNOilTempRear = TBDRNOilTempRear_q, BRNOilTemp2 = BRNOilTemp2_q, BRNOilTemp3 = BRNOilTemp3_q, BRNOilTemp4 = BRNOilTemp4_q, BRNOilTemp5 = BRNOilTemp5_q, BRNOilTemp6 = BRNOilTemp6_q, BRNOilTemp7 = BRNOilTemp7_q, SealOilPressureExciteEnd = SealOilPressureExciteEnd_q, SealOilPressureTurbineEnd = SealOilPressureTurbineEnd_q, Username = Username_q, Index = Index_q }; 
      } 

      connection.Close(); 
      return BoilerReading;   
     } 

     BoilerReading[] BoilerReadings = new BoilerReading[] 
     { 
      new BoilerReading { TimeStamp = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
     }; 

     public IEnumerable<BoilerReading> GetAllBoilerReadings() 
     { 
      return BoilerReadings; 
     } 

     public IHttpActionResult BoilerReading(int id) 
     { 
      var BoilerReading = BoilerReadings.FirstOrDefault((p) => p.Id == id); 
      if (BoilerReading == null) 
      { 
       return NotFound(); 
      } 
      return Ok(BoilerReading); 
     } 
    } 
} 

はしかし、私は私がのexecuteQuery()関数を持っているクエリで静的配列を交換したいが、私はそれを把握することはできませんでる。私はいくつかの方法で試しました。私はコードを追加して、配列部分に直接問い合わせを行い、変数を変更しようとしましたが、コンパイラはそれを好きではないようです。 ExecuteQuery()を呼び出そうとするたびに。照会されたデータを渡すと動作しません。

これらの配列をクエリされたデータと置き換えて動的なものにする適切な方法は何ですか?

SQLサーバーからの動的クエリを使用します。

+1

開始するにはいくつかのヒント:あなたの質問は、クエリから配列を埋める方法です、RESTとasp.netについては気を散らしています。実際に 'TOP 1'だけが必要な場合は、配列は必要ありません。プロパティとして' BoilerReading'が必要です。 'dt.Rows [0] [" whatever "]'から同じ値を割り当てるには、同じ構文( 'new BoilerReading {...')を使います。 – Crowcoder

+0

私はそれを試みましたが、私はREST APIがどのようにボイラーの読みを得ることができるのか分かりません。 ExecuteQuery()の内部に新しいボイラーの読み込みを入れると、IEnumerable GetAllBoilerReadings()は機能しますか?これを示すために私のコードを更新しました。私がこのようにしようとすると、 'メソッドグループ' BoilerReading 'を非代理人型' BoilerReading 'に変換できないので、実行されません。 – user1813298

+0

'BoilerReading'メソッドは、1つのアイテムを返すことを意味します。 'GetAllBoilerReadings()'とは何ですか?あなたは1つ、またはすべてをしたいですか?メソッドをクラスと同じ名前にしないでください。したがって、 'BoilerReading(int id)'の代わりに 'Get'または' GetBoilerReading'だけを使用してください。 – Crowcoder

答えて

0

まず、メソッド名をクラスと同じ名前にしないでください。

第2に、BoilerReadingを返すメソッドがあります。それを呼び出すだけです。私はこのように優れた設計を推進しておりません

public BoilerReading ExecuteQuery() 
{ 
    var MyBoilerReading = new BoilerReading(); 
    DataTable dt = new DataTable(); 
    SqlConnection connection = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=DNN;User ID=sa;Password=*****;"); 
    connection.Open(); 
    SqlCommand sqlCmd = new SqlCommand("select top 1 * from DNN.dbo.avtActionForm_BoilerReadingsLog order by TimeStamp", connection); 
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); 
    sqlDa.Fill(dt); 
    if (dt.Rows.Count > 0) 
    { 
     DateTime TimeStamp_q = (DateTime)dt.Rows[0]["TimeStamp"]; 
     int TurbineChosen_q = (int)dt.Rows[0]["TurbineChosen"]; 
     decimal MOPSuctionPressure_q = (decimal)dt.Rows[0]["MOPSuctionPressure"]; 
     decimal LubeOilPressure_q = (decimal)dt.Rows[0]["LubeOilPressure"]; 
     decimal ControlOilPressure_q = (decimal)dt.Rows[0]["ControlOilPressure"]; 
     .... etc. 
     MyBoilerReading.Timestamp = TimeStamp_q; 
     MyBoilerReading.TurbineChosen = TurbineChosen_q; 
     ... etc.  
    } 

    connection.Close(); 
    return MyBoilerReading; //return the instance that you actually populate with values.  
} 

注:それは実際にBoilerReadingインスタンスを返すよう

public IHttpActionResult GetBoilerReading(int id) 
{ 
    var BoilerReading = ExecuteQuery(); //you will probably want this to take an int parameter and add a WHERE clause to your query. 
    if (BoilerReading == null) 
    { 
     return NotFound(); 
    } 
    return Ok(BoilerReading); 
} 

は今のexecuteQueryを修正します。より堅牢なエラーとヌルチェックが必要になります。 Disposedである必要があるリソース(Connections、Commandsなど)も使用しています。

関連する問題