【问题标题】:windows service integration with sql using ado .net使用 ado .net 与 sql 的 windows 服务集成
【发布时间】:2017-09-13 07:29:43
【问题描述】:
 **public  static void göster()
    {
        SqlConnection connection =GetConnection.GetConnectionObject();


        //TODO parametreleri command.parameters yoluyla alsın
        SqlCommand command = new SqlCommand();
        command.CommandText= "select * from windowsserv";
        command.Connection = connection;
        if (connection.State != ConnectionState.Open)
            connection.Open();
        Console.WriteLine("Connection opened");
        SqlDataReader rdr = command.ExecuteReader();

        while(rdr.Read())
        {
    ------------------> Console.WriteLine(rdr[0].ToString()+"--"+rdr[1].ToString());

        }

        if (connection.State != ConnectionState.Closed)
            connection.Close();
        Console.WriteLine("Connection Closed.");
        Console.ReadLine();

    }

大家好,这是我的代码,还有更多我没有显示的,我将数据记录到 sql server。此代码块(用箭头指向)显示所有 sql server 数据,我只想在控制台中查看最后一个数据(记录到 sql server)。但是我找不到任何答案。请帮忙。**

【问题讨论】:

  • 这是一个很奇怪的问题。从您的问题中无法判断“最后数据”的定义应该是什么。

标签: c# .net sql-server ado


【解决方案1】:

在这一行

command.CommandText= "select * from windowsserv";

您可以使用一些 SQL 过滤器和排序,例如:(这将根据日期过滤器返回最后 10 个寄存器。现在您需要知道您的(最后一个数据)已记录的范围,以准确获得您需要的内容。 将 (date_column) 替换为表中具有此类数据的列。

select top 10 * from windowsserv where (date_column) between '2017-09-12 00:00:00' and '2017-09-13 00:00:00' order by (date_column) DESC

然后在你的代码中你将有这样的东西:

SqlCommand command = new SqlCommand();
command.CommandText = "select * from select top 10 * from windowsserv where (date_column) between \'2017-09-12 00:00:00\' and \'2017-09-13 00:00:00\' order by (date_column) DESC";

您也可以这样做来改进您的代码:

        using (SqlConnection connection = GetConnection.GetConnectionObject())
        {
            //TODO parametreleri command.parameters yoluyla alsın
            var command = new SqlCommand
            {
                CommandText = "select * from windowsserv",
                Connection = connection
            };

            if (connection.State != ConnectionState.Open)
                connection.Open();

            Console.WriteLine("Connection opened");
            var rdr = command.ExecuteReader();

            while (rdr.Read())
            {
                Console.WriteLine(rdr[0] + "--" + rdr[1]);
            }
        }

【讨论】:

  • 非常感谢您的推荐,我也使用了 SQL 过滤器。我的新代码块是 """"command.CommandText= select top 1 * from windowsserv order by date desc ;"""""" 而不是 """""command.CommandText= select * from windowsserv";"""" "。
【解决方案2】:

尝试将您的 while 循环更改为。

    bool isLast = rdr.Read();
    while(isLast)
    {
        string firstCol = rdr[0].ToString();
        string secondCol = rdr[1].ToString();
        isLast = rdr.Read();
        if(!isLast)
        {
            Console.WriteLine(firstCol+"--"+secondCol);
            break;
        }
    }

【讨论】:

    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多