windows 使用 ado .net 与 sql 进行服务集成
windows service integration with sql using ado .net
**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 服务器。此代码块(用箭头指向)显示所有 sql 服务器数据,我只想在控制台中查看最后的数据(记录到 sql 服务器)。但是我找不到任何答案。请帮忙。**
尝试将您的 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;
}
}
在这一行
command.CommandText= "select * from windowsserv";
您可以使用一些 SQL 过滤器和排序,例如:(这将 return 基于日期过滤器注册最后 10 个。现在您需要知道您的(最后数据)的范围已被记录下来,以得到你所需要的。
将 (date_column) 替换为您 table 拥有此类数据的列。
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]);
}
}
**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 服务器。此代码块(用箭头指向)显示所有 sql 服务器数据,我只想在控制台中查看最后的数据(记录到 sql 服务器)。但是我找不到任何答案。请帮忙。**
尝试将您的 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;
}
}
在这一行
command.CommandText= "select * from windowsserv";
您可以使用一些 SQL 过滤器和排序,例如:(这将 return 基于日期过滤器注册最后 10 个。现在您需要知道您的(最后数据)的范围已被记录下来,以得到你所需要的。 将 (date_column) 替换为您 table 拥有此类数据的列。
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]);
}
}