c#实时读取数据库的问题
我现在数据库一更新数据就全都读了,但我想只读后面的,比如1-4000,2-4001,3-4002,一共一直只读4000组数。这该怎么办,我用的是sql数据库。这是我现在程序代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace shishi
{
class Program
{
private static string _connStr;
static void Main(string[] args)
{
_connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
SqlDependency.Start(_connStr);//传入连接字符串,启动基于数据库的监听
UpdateGrid();
Console.Read();
}
private static void UpdateGrid()
{
using (SqlConnection connection = new SqlConnection(_connStr))
{
//依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
using (SqlCommand command = new SqlCommand("select test From [dbo].[shishitb]", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
SqlDataReader sdr = command.ExecuteReader();
Console.WriteLine();
while (sdr.Read())
{
Console.WriteLine("test:{0}", sdr["test"].ToString());
}
sdr.Close();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
UpdateGrid();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace shishi
{
class Program
{
private static string _connStr;
static void Main(string[] args)
{
_connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
SqlDependency.Start(_connStr);//传入连接字符串,启动基于数据库的监听
UpdateGrid();
Console.Read();
}
private static void UpdateGrid()
{
using (SqlConnection connection = new SqlConnection(_connStr))
{
//依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
using (SqlCommand command = new SqlCommand("select test From [dbo].[shishitb]", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
SqlDataReader sdr = command.ExecuteReader();
Console.WriteLine();
while (sdr.Read())
{
Console.WriteLine("test:{0}", sdr["test"].ToString());
}
sdr.Close();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
UpdateGrid();
}
}
}
...全文
相关推荐
c# 数据库操作
**自动读取和修改**:描述中提到的软件功能可能涉及监听数据库变化并实时显示在界面上,这可以通过轮询数据库或使用特定的数据库触发器实现。
C#与SQL数据库交互:实时读取与显示表格数据
在现代软件开发中,数据的存储和读取是不可或缺的环节。数据库提供了存储、管理及检索数据的机制,而C#作为.NET框架的核心语言,提供了丰富的API用于与数据库进行交互。本章节首先介绍如何使用C#从数据库中读取表格数据的基础知识。数据缓存是一种将数据临时存储在快速访问内存中的技术,目的是减少对后端存储系统(如数据库)的访问次数。缓存可以显著提高应用性能,尤其是在数据读取操作频繁的场景中。通过缓存数据,可以在需要时快速检索数据,减少数据加载时间,从而提升用户体验。
C#读取数据库中表格数据
通过合理设计和优化,我们可以实现高效的数据读取和实时显示,满足不同应用场景的需求。不过,实际项目中可能还需要考虑错误处理、性能优化、数据缓存等问题,这些都是提升应用程序稳定性和效率的关键。
Begining C# database
在“Beginning C# Database”这个主题中,我们将深入探讨如何使用C#进行数据库编程,这对于构建数据驱动的应用程序至关重要。
Database Generator
"Database Generator"是一款基于C#编程语言开发的数据库生成工具,专为简化数据库创建过程而设计。


