If you need a list of your objects then you need to define one, fill it while you read from the database and return it from your method.
The problem with the max value (assuming that you consider for max value the date contained in the LastStatusUpdate could be solved passing an out variable of type DateTime
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} {1} {2} {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;
}
}
}
Now you can call this method in this way
List<TheQFromDB> result = GetQ(out DateTime lastUpdateValue);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(lastUpdateValue.ToString());
foreach(var q in result)
{
Console.WriteLine("{0} {1} {2} {3} ={4}", q.ID, q.PrinterName,
q.AddedToQueue.ToString(), q.LastStatusUpdate,
q.SecondsDiff+"sec");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…