Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

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

How to save my data from Sql in a List ?! i would like to save the data which are holding in an Instance from the class in a List then find the max of value ...take a look at code please !

public class 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}	{1}			{2}		{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}	{1}			{2}		{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;

            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...