如何存储在控制台应用程序中显示的输出,并将其存储在变量中并将其与新的输出变量匹配 [英] How to store an output that displays in console application, and store it in a Variable and Match it with new output variable

查看:70
本文介绍了如何存储在控制台应用程序中显示的输出,并将其存储在变量中并将其与新的输出变量匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c#控制台应用程序,它每5秒执行一次SQL查询并在控制台中显示输出。因此,在执行查询并获得第一个输出后,它将清除第一个输出并在第一个位置写入新输出。



现在,我需要存储当它出现时的第一个输出,在变量中,当第二个输出到来时,它应该检查输出是否与第一个相同。如果匹配没有问题,如果它不匹配则应该显示一行值已更改。



没有必要匹配整个输出,只是我需要第一列,即代码中的reader [0]的值。如果读取器[0]值从以前的读卡器[0]更改,那么它应该打印一行值已更改



我的控制台应用程序是: -



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)
  {
   // 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);
              }
          }
          }
      }
    }

推荐答案

所以创建一个数组或其他集合,其中包含您第一次读取的值的数量。第一次填充它,然后比较后续的。



但请不要这样做!

从写作开始返回包含所有第一列,n值的集合的方法。

第一次调用它,并在类级别保存集合。

然后添加一个Timer到您的代码,并将其间隔设置为5000(此时为5秒)并处理它的Tick事件。

您的主代码然后变成一个简单的Console.ReadKey退出程序。

在Tick处理程序中,再次调用该方法以获取新集合,并逐项比较,报告任何差异。
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.


我还没有测试过它你可以测试它,让我知道。选择第一列中不存在的-999值。另外我假设reader [0]持有字符串值。



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.

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


这篇关于如何存储在控制台应用程序中显示的输出,并将其存储在变量中并将其与新的输出变量匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆