[英]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.