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. I've tried making an SqlCommand where it asks everything from the View containers. How do I set information from the containers view to a different model(for example B0[Product] and Bb[Vessel]) and display it in one view?
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;
}
}
}
I will use ViewData to grab the information and so I can use that in my Index View.
Your "model" should be a custom class that can hold both informations (Date and VesselName):
public class SomeModel
{
public DateTime Date {get;set;}
public string VesselName {get;set;}
}
(Note that I used DateTime
instead of string
because I feel it's more appropriate to store a date)
Then you could create one model instead of b0
and bb
:
SomeModel myModel = new SomeModel();
And set both properties.
myModel.Date = (DateTime)sdr["Date"];
myModel.VesselName = (string)sdr["Vessel_Name"];
Then you can use Context.Add(myModel);
(note Context
and not SomeModel
) and here you are.
In short, your while
loop would look like this:
while (sdr.Read())
{
SomeModel myModel = new SomeModel();
myModel.Date = (DateTime)sdr["Date"];
myModel.VesselName= (string)sdr["Vessel_Name"];
Context.Add(myModel);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.