簡體   English   中英

如何將 SQL 中的數據保存在列表中,然后僅使用 C# 而不使用 LINQ 查找最大值

[英]How to save the Data from SQL in a List then find the max only using C# and no LINQ

如何將 Sql 中的數據保存在列表中?! 我想將保存在類中的實例中的數據保存在列表中,然后找到最大值...請看一下代碼!

公共類 PrintQueue

{
    public static TheQFromDB GetQ()

    {
        TheQFromDB TheQ = new TheQFromDB();

        using (SqlConnection connection = DBConnection.GetTheQFromDB())
        {
            connection.Open();

            SqlCommand command = new SqlCommand("select *from PrintQueue WHERE AddedToQueue  >= DATEADD (day, -2, GetDate()) ", connection);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("{0}\t{1}\t\t\t{2}\t\t{3}", reader.GetName(0), reader.GetName(1), reader.GetName(4), reader.GetName(5));
                    while (reader.Read())
                    {
                        TheQ.Id = (Int32)reader["Id"];
                        TheQ.PrinterName = (string)reader["PrinterName"];
                        TheQ.AddedToQueue = (DateTime)reader["AddedToQueue"];
                        TheQ.LastStatusUpdate = (DateTime)reader["LastStatusUpdate"];
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("{0}\t{1}\t\t\t{2}\t\t{3} ={4}", reader.GetSqlInt32(0), reader.GetSqlString(1), reader.GetSqlDateTime(4), reader.GetSqlDateTime(5), TheQ.SecondsDiff+"sec");
                        Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
                    }
                    Console.ReadKey();
                    return TheQ;
                }

                return null;

            }
        }

如果您需要一個對象列表,那么您需要定義一個對象,在從數據庫讀取時填充它並從您的方法中返回它。

最大值的問題(假設您考慮最大值, LastStatusUpdate 中包含的日期可以通過傳遞 DateTime 類型的out變量來解決

public static List<TheQFromDB> GetQ(out DateTime maxValue)
{
    // Initialize the maxValue 
    maxValue = DateTime.MinValue;

    // The list with your objects....
    List<TheQFromDB> elements = new List<TheQFromDB>();

    using (SqlConnection connection = DBConnection.GetTheQFromDB())
    {
        connection.Open();
        SqlCommand command = new SqlCommand("select *from PrintQueue WHERE AddedToQueue  >= DATEADD (day, -2, GetDate()) ", connection);

        using (SqlDataReader reader = command.ExecuteReader())
        {
            // Console.ForegroundColor = ConsoleColor.Yellow;
            // Console.WriteLine("{0}\t{1}\t\t\t{2}\t\t{3}", reader.GetName(0), reader.GetName(1), reader.GetName(4), reader.GetName(5));

            // Loop over the data returned by the reader
            while(reader.Read())
            {
                // Build the current instance for the current record
                // Do not declare this object outside the loop or your list
                // will have the same values from the last record
                TheQFromDB TheQ = new TheQFromDB();
                TheQ.Id = (Int32)reader["Id"];
                TheQ.PrinterName = (string)reader["PrinterName"];
                TheQ.AddedToQueue = (DateTime)reader["AddedToQueue"];
                TheQ.LastStatusUpdate = (DateTime)reader["LastStatusUpdate"];

                // Now check if the current record is 'greater' than 
                // a saved value from a previous record.
                if(TheQ.LastStatusUpdate > maxValue)
                   maxValue = TheQ.LastStatusUpdate;


                // Add the current element to the list
                elements.Add(TheQ);
            }

            // Return to the caller.
            return elements;
        }
     }
}

現在你可以這樣調用這個方法

List<TheQFromDB> result = GetQ(out DateTime lastUpdateValue);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(lastUpdateValue.ToString());
foreach(var q in result)
{
    Console.WriteLine("{0}\t{1}\t\t\t{2}\t\t{3} ={4}", q.ID, q.PrinterName,  
                     q.AddedToQueue.ToString(), q.LastStatusUpdate,  
                     q.SecondsDiff+"sec");
}    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM