简体   繁体   English

如何在 ASP.NET 内核中显示 mysql 查看

[英]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. 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.我尝试制作一个 SqlCommand,它会从 View 容器中询问所有内容。 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?如何将容器视图中的信息设置为不同的模型(例如 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;
                    }
            }
        }

I will use ViewData to grab the information and so I can use that in my Index View.我将使用 ViewData 来获取信息,因此我可以在我的索引视图中使用它。

Your "model" should be a custom class that can hold both informations (Date and VesselName):你的“模型”应该是一个自定义的 class 可以保存两个信息(日期和船舶名称):

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) (请注意,我使用DateTime而不是string ,因为我觉得存储日期更合适)

Then you could create one model instead of b0 and bb :然后你可以创建一个model 而不是b0bb

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);然后你可以使用Context.Add(myModel); (note Context and not SomeModel ) and here you are. (注意Context而不是SomeModel ),你在这里。


In short, your while loop would look like this:简而言之,您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM