简体   繁体   English

如何从数据库获取数据,将其存储为List <>并显示结果以进行查看

[英]How to get data from DB, store as List<>, and display the results to view

I am trying to get data as a list from the database but it is not showing any results. 我正在尝试从数据库中获取数据作为列表,但未显示任何结果。 I tried to debug but after this line it doesn't let me to step over / F10 : 我尝试调试,但在此行之后,它不允许我step over / F10

DataSet ds = new DataSet();
da.Fill(ds);

I am trying to do this by following this example on here: link 1 and here link 2 but finding it difficult hence I thought that I should ask here. 我正在尝试通过以下示例进行操作: 链接1链接2,但是发现很困难,因此我认为我应该在这里询问。

Can someone please explain why it is not displaying the results and as such what I may be doing wrong here? 有人可以解释为什么它不显示结果,因此我在这里做错了吗? how do I work around and achieve it to display the data? 如何解决并实现它以显示数据?

Here's the full controller code for your inspection: 这是完整的控制器代码供您检查:

public static List<DBTrack> GetListOfTracks()
{
    if (DBTrackData == null)
    {
       string myConnectionString = "Data Source="; // I have taken off the source
       string mySelectString = "SELECT TrackID, AddedDate, TrackName, ArtistName from TBL_Track";
       SqlDataAdapter da = new SqlDataAdapter();
       DataSet ds = new DataSet();
       da.Fill(ds);

       OleDbConnection myConnection = new OleDbConnection(myConnectionString);
       OleDbCommand myCommand = new OleDbCommand(mySelectString, myConnection);
       myCommand.Connection.Open();
       OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

       List<DBTrack> list = new List<DBTrack>();
       while (myReader.Read())
       {
           DBTrack data = new DBTrack();
           data.TrackID = (Guid)(myReader["TrackID"]);
           data.AddedDate = (DateTime)myReader["AddedDate"];
           data.TrackName = (string)myReader["TrackName"];
           data.ArtistName = (string)myReader["ArtistName"];
           list.Add(data);
       };
    }

   return DBTrackData;
}

EDIT: 编辑:

SqlConnection mySQLconnection = new SqlConnection(@"Data Source=server-2\SQLExpress;Initial Catalog=End;Integrated Security=False;User ID=test1;Password=**");
SqlCommand mySelectString = new SqlCommand("SELECT TrackID, AddedDate, TrackName, ArtistName from TBL_Track");

using (SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();

using (SqlCommand myCommand = new SqlCommand // The best overload method System.Data.SqlClient.SqlCommand.SqlCommand has some invalid arguments
(mySelectString, mySQLconnection))           // Cannot convert from System.Data.SqlClient.SqlCommand to 'string' 
 {

You should set the DBTrackData to the result of the list: 您应该将DBTrackData设置为列表的结果:

while (myReader.Read())
{
    ...
}

DBTrackData = list;

You could also directly edit the DBTrackData in your code: 您也可以在代码中直接编辑DBTrackData

private static List<DBTrack> DBTrackData
public static List<DBTrack> GetListOfTracks()
{
    if (DBTrackData == null)
    {
       ...

       DBTrackData = new List<DBTrack>();
       while (myReader.Read())
       {
           DBTrack data = new DBTrack();

           ...

           DBTrackData.Add(data);
       };
    }

   return DBTrackData;
}

So, in full it should be: 因此,完整的应该是:

public static List<DBTrack> GetListOfTracks()
{
    try
    {
        if (DBTrackData == null)
        {
            string myConnectionString = "Data Source="; // I have taken off the source
            string mySelectString = "SELECT TrackID, AddedDate, TrackName, ArtistName from TBL_Track";

            using (OleDbConnection myConnection = new OleDbConnection(myConnectionString))
            {
               myConnection.Open();

               using (OleDbCommand myCommand = new OleDbCommand(mySelectString, myConnection))
               {
                   OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

                   List<DBTrack> list = new List<DBTrack>();

                   while (myReader.Read())
                   {
                       DBTrack data = new DBTrack();
                       data.TrackID = (Guid)(myReader["TrackID"]);
                       data.AddedDate = (DateTime)myReader["AddedDate"];
                       data.TrackName = (string)myReader["TrackName"];
                       data.ArtistName = (string)myReader["ArtistName"];

                       list.Add(data);
                   }

                   DBTrackData = list;
               }
            }
        }

        return DBTrackData;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    return DBTrackData;
}

Your main problem is that you are using two different ways of getting the data, but the first is only partially implemented. 您的主要问题是您正在使用两种不同的方式来获取数据,但是第一种仅部分实现。 You are using a data adapter and a data reader. 您正在使用数据适配器和数据读取器。 Your data adapter isn't even being passed a connection or query, so this is why it is not working. 您的数据适配器甚至都没有传递连接或查询,因此这就是它不起作用的原因。 So you can just remove that part of the code. 因此,您只需删除部分代码即可。

It's also easier to read if you immediately return when DBTrackData is not null rather than have a big if block over the whole code. 如果您在DBTrackData不为null时立即返回,而不是在整个代码中使用较大的if块,则更容易阅读。 You will then have something like: 然后,您将获得类似以下内容的信息:

public static List<DBTrack> GetListOfTracks()
{
   if (DBTrackData != null) return DBTrackData;

   string myConnectionString = "Data Source="; // I have taken off the source
   string mySelectString = "SELECT TrackID, AddedDate, TrackName, ArtistName from TBL_Track";

   OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(mySelectString, myConnection);
   myCommand.Connection.Open();
   OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

   List<DBTrack> list = new List<DBTrack>();
   while (myReader.Read())
   {
       DBTrack data = new DBTrack();
       data.TrackID = (Guid)(myReader["TrackID"]);
       data.AddedDate = (DateTime)myReader["AddedDate"];
       data.TrackName = (string)myReader["TrackName"];
       data.ArtistName = (string)myReader["ArtistName"];
       list.Add(data);
   };

   //Setting DBTrackData means these values will get returned on every call to GetListOfTracks for this instance of the class
   DBTrackData = list;

   return list;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在视图中将2个不同表中的数据显示为一个列表 - How to display data from 2 different table as one list in view 如何将作为列表的foreach结果存储到变量中并在弹出窗口中显示 - How to store results of the foreach which is a list into variable and display it in popup window 从列表中获取数据并显示在视图中 - Fetch data from a List and display in View 如何在视图中显示基于月份的列表数据 - How to display list data based on month in View 当数据来自while循环函数并显示在列表视图中时,如何在SQL Server表中存储数据? - How to store data in a SQL Server table when the data comes from a while loop function and shown in a list view? C#如何将数据列表从视图中获取到MVC 3中的控制器 - C# how to get list of data from view into controller in MVC 3 如何获取从服务获取的ViewModel列表中的数据,然后更新View? - How to get data to ViewModel list that is acquired from a service and then update the View? 如何从MVC中的3个表中查看结果 - How to get results in View from 3 tables in MVC 如何在列表中显示控制器和动作? - How to display controllers and actions from list in view? 如何从 controller 传递列表以查看和显示 - How to pass list from controller to view and display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM