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:-
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)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "server=(local);database=modelDb;user id=sa;pwd=123456";
conn.Open();
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);
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);
}
}
}
}
}