如何从DB获取数据,以List<>存储,并显示要查看的结果

时间:2014-06-16 15:03:46

标签: c# database list

我正在尝试将数据作为数据库中的列表获取,但它没有显示任何结果。我试图调试但是在这一行之后它并没有让我去step over / F10

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

我试图通过以下示例执行此操作:link 1和此处link 2但发现很难,因此我认为我应该在这里问。

有人可以解释为什么它没有显示结果,因此我可能在这里做错了吗?我如何解决并实现它以显示数据?

这里是您检查的完整控制器代码:

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;
}

修改

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' 
 {

2 个答案:

答案 0 :(得分:2)

您应该将DBTrackData设置为列表的结果:

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

DBTrackData = list;

您还可以直接编辑代码中的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;
}

所以,完整它应该是:

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;
}

答案 1 :(得分:2)

您的主要问题是您使用两种不同的方式获取数据,但第一种方法只是部分实现。您正在使用数据适配器和数据读取器。您的数据适配器甚至没有传递连接或查询,因此这就是它无法正常工作的原因。所以你可以删除那部分代码。

如果在DBTrackData不为null时立即返回,而不是在整个代码上有一个大的if块,那么它也更容易阅读。然后你会有类似的东西:

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;
}