110,535
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static string connectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
static void Main(string[] args)
{
SqlDependency.Start(connectionString);//传入连接字符串,启动基于数据库的监听
UpdateGrid();
Console.Read();
}
public static void UpdateGrid()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
using (SqlCommand command = new SqlCommand("select ID,UserID,[Message] From [dbo].[Messages]", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
command.ExecuteNonQuery();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
//UpdateGrid();
//这里只能扑获到第一次插入数据,后面在插入的数据就无法扑获到了。
string sql = "select TOP 1 ID,UserID,[Message] From [dbo].[Messages] order by ID desc";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, connection))
{
try
{
connection.Open();
SqlDataReader sdr = cmd.ExecuteReader();
Console.WriteLine();
while (sdr.Read())
{
Console.WriteLine("Id:{0}\tUserId:{1}\tMessage:{2}", sdr["ID"].ToString(), sdr["UserId"].ToString(),
sdr["Message"].ToString());
}
sdr.Close();
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
}
}
CREATE TABLE [dbo].[Messages](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Message] [nvarchar](256) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
)
你的数据字段1 你的数据字段2 你的数据字段3 你的数据字段4 你的数据字段5 ………… 你的数据字段n 状态字段(未处理为Start)
然后你就可以每隔一定时间轮询一下状态为 Start 的该表全部数据,在轮询到你的程序后将状态变更为 In Presses ,这样你下次轮询就不会找到这些数据。
后面你怎么处理那就是你的业务逻辑了。insert into Users(uname)
select '2'
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string conStr = "server=.;database=movies;uid=sa;pwd=sasa;";
private void Form1_Load(object sender, EventArgs e)
{
SqlDependency.Start(conStr);
Update(conStr);
}
public void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
Update(conStr);
}
private void Update(string conStr)
{
using (SqlConnection connection = new SqlConnection(conStr))
{
//此处 要注意 不能使用* 表名要加[dbo] 否则会出现一直调用执行 OnChange
string sql = "select id from [dbo].[Users]";
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
command.CommandType = CommandType.Text;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
//必须要执行一下command
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
this.dataGridView1.Invoke(new Action(() => { this.dataGridView1.DataSource = ds.Tables[0]; }));
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
while (sdr.Read())
{
Console.WriteLine("Id:{0}\tUserId:{1}\tMessage:{2}", sdr["ID"].ToString(), sdr["UserId"].ToString(),
sdr["Message"].ToString());
//break;//为了完成目的这样处理了一下。查一条就跳出。
}
@小疯纸纸 不是这样的啊!我试过了,我只要insert语句,它就会把表里所有数据都打印出来。