[英]How to display a mysql View in ASP.NET Core
I've created a MySQL View from a database in Microsoft SQL Server Management Studio and I would like to get the information in the MySQL View to my Asp.net controller. 我嘗試制作一個 SqlCommand,它會從 View 容器中詢問所有內容。 如何將容器視圖中的信息設置為不同的模型(例如 B0[Product] 和 Bb[Vessel])並在一個視圖中顯示?
public IActionResult TablesColumnDisplay()
{
List<SomeModel> Context = new List<SomeModel>(); // i dont know what model i should be using, or no model whatsoever?
using (SqlConnection SqlConn = new SqlConnection(conn))
{
using (SqlCommand SqlComm = new SqlCommand("SELECT * FROM containers;")) //Containers is the final MySQL View
{
using (SqlCommand SqlComm = new SqlCommand("SELECT * FROM containers;")) //Containers is the final MySQL View
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
{
SqlComm.Connection = SqlConn;
SqlConn.Open();
sda.SelectCommand = SqlComm;
SqlDataReader sdr = SqlComm.ExecuteReader();
while (sdr.Read())
{
B0 b0 = new B0();
Bb bb = new Bb();
b0.Date = (string)sdr["Date"];
bb.VesselName= (string)sdr["Vessel_Name"];
SomeModel.Add(b0, bb); //doesn't take more than 1 arguments
}
}
return Context;
}
}
}
我將使用 ViewData 來獲取信息,因此我可以在我的索引視圖中使用它。
你的“模型”應該是一個自定義的 class 可以保存兩個信息(日期和船舶名稱):
public class SomeModel
{
public DateTime Date {get;set;}
public string VesselName {get;set;}
}
(請注意,我使用DateTime
而不是string
,因為我覺得存儲日期更合適)
然后你可以創建一個model 而不是b0
和bb
:
SomeModel myModel = new SomeModel();
並設置這兩個屬性。
myModel.Date = (DateTime)sdr["Date"];
myModel.VesselName = (string)sdr["Vessel_Name"];
然后你可以使用Context.Add(myModel);
(注意Context
而不是SomeModel
),你在這里。
簡而言之,您的while
循環將如下所示:
while (sdr.Read())
{
SomeModel myModel = new SomeModel();
myModel.Date = (DateTime)sdr["Date"];
myModel.VesselName= (string)sdr["Vessel_Name"];
Context.Add(myModel);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.