我試圖在這裏本教程: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服務器的動態查詢。
有些技巧可以讓你開始:你的問題是如何從查詢中填充數組,關於REST和asp.net的東西令人分心。如果你真的只需要'TOP 1',那麼你不需要一個數組,你只需要'BoilerReading',可能作爲一個屬性。使用相同的語法('new BoilerReading {...')從'dt.Rows [0] [「whatever」]賦值。 – Crowcoder
我試過了,但我想我不明白REST API將如何獲取該鍋爐讀數。 IEnumerable GetAllBoilerReadings()是否仍然可以工作,如果我把新的鍋爐讀數放在ExecuteQuery()中?我已更新我的代碼以顯示此內容。當我嘗試這樣做時,我得到'無法將方法組'BoilerReading'轉換爲非委託類型'BoilerReading',因此它不能運行。 –
user1813298
'BoilerReading'方法意味着你想返回一個項目。 GetAllBoilerReadings()是什麼?你想要一個還是全部?並且不要將方法命名爲類。因此,而不是'BoilerReading(int id)',只需使用'Get'或'GetBoilerReading'。 – Crowcoder