Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a c# console application which executes an SQL Query for every 5 Seconds and display the output in the Console. So after executing the query and getting the first output, it will clear the first Output and write new output in the place of first.

Now, I need to store that first output when it comes, in a variable and when 2nd output comes it should check whether the output is same as first one or not. If matches no issues, if it doesn't match it should display a line "The Value is changed".

There is no need to match entire output, Just i need first column i.e., value of reader[0] in the code. if reader[0] value is changed from previous reader[0] then it should print a line "The values are changed"

My Console Application is:-

C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace LocationFinder
{
 class Program
 {
     public static void sleep(int ms)
     {
         DateTime startTime_loc = DateTime.Now;
         DateTime stopTime_loc;
         TimeSpan duration_loc;
         do
         {
             stopTime_loc = DateTime.Now;
             duration_loc = stopTime_loc - startTime_loc;
         } while ((duration_loc.TotalMilliseconds < ms) && (!Console.KeyAvailable)); ;
     }
  static void Main(string[] args)
  {
   // Create the connection to the resource!
   // This is the connection, that is established and
   // will be available throughout this block.
     
          using (SqlConnection conn = new SqlConnection())
          {
              // Create the connectionString
              // Trusted_Connection is used to denote the connection uses Windows Authentication
              conn.ConnectionString = "server=(local);database=modelDb;user id=sa;pwd=123456";
              conn.Open();

              // Create the command
              SqlCommand command = new SqlCommand("DECLARE @var varchar(1000) = (SELECT TOP 1 Text FROM Alarms WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC) DECLARE @start_position int, @end_position int SELECT @start_position = PATINDEX('% at%', @var) SELECT @end_position = PATINDEX('%kilometers%', @var) DECLARE @VALUE VARCHAR(10) = (Select SUBSTRING(@var, @start_position+5,5)) Select Top 1 @VALUE,RouteTable.Latitude,Routetable.Longitude,Alarms.ApplicationTime FROM Alarms INNER JOIN Routetable ON Routetable.Location BETWEEN FLOOR(@VALUE)-1 AND CEILING(@VALUE)+1 WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC", conn);

              /* Get the rows and display on the screen! 
               * This section of the code has the basic code
               * that will display the content from the Database Table
               * on the screen using an SqlDataReader. */
              do
              {
                  using (SqlDataReader reader = command.ExecuteReader())
                  {
                      Console.WriteLine("Location\t\t  Latitude\t\t Longitude\t\t Time");
                      while (reader.Read())
                      {
                          Console.WriteLine(String.Format("{0}\t\t |\t{1}\t|\t{2}\t|\t{3}", reader[0], reader[1], reader[2], reader[3]));
                      }
                  }
                  sleep(5000);
                  Console.Clear();
              }
              while (!Console.KeyAvailable);
              {
                 Console.ReadKey(true);
              }
          }
          }
      }
    }
Posted
Updated 14-Apr-15 23:19pm
v3
Comments
Member 15491901 10-Jan-22 3:30am    
I Have an application that shows output of cpu utilization its memory used and current date and time in console application output now i want to store this 3 things in my database how can i store it?

So create an array or other collection which holds the number of values you read the first time. Populate it the first time, and compare on subsequent ones.

But please, don't do it like that!
Start by writing a method which returns a collection containing all the first colum,n values.
Call it the first time, and save the collection at class level.
Then add a Timer to your code, and setting it's interval to 5000 (5 seconds, as you do at the moment) and handling it's Tick event.
You main code then becomes just a simple Console.ReadKey to exit the program.
In the Tick handler, call the method again to get a new collection, and compare it item by item, reporting any differences.
 
Share this answer
 
I haven't tested it so can you test it and let me know. Choose a value for "-999" that doesn't exist in your first column. Also I assume that reader[0] holds string value.

C#
String fRec = "-999";
String nRec = "-999";
while (reader.Read())
{
    if (fRec != "-999")
    {
        nRec = fRec;
    }
    fRec = Convert.ToString(reader[0]);
    if (nRec != fRec && nRec != "-999")
    {
        Console.WriteLine("The value is changed");
    }
    else
    {
      Console.WriteLine(String.Format("{0}\t\t |\t{1}\t|\t{2}\t|\t{3}", reader[0], reader[1], reader[2], reader[3]));
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900