Sql查询通知 [英] Sql query Notification
本文介绍了Sql查询通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现一个功能,当一个用户在另一个用户修改记录时通知数据库中的更改。
我参考的参考文章
使用SqlDependency和SqlCacheDependency查询通知 [ ^ ]
I am trying to implement a feature where in One user is notified on the change in the database when another user modifies the record .
The reference article i took is
Query Notification using SqlDependency and SqlCacheDependency[^]
protected void btn_comment_Click(object sender, EventArgs e)
{
try
{
//if (!CheckUserPermissions())
//
DateTime dtt = DateTime.Now;
dtt.Date.ToString("MM/dd/yyyy");
cn.ConnectionString = ConfigurationManager.ConnectionStrings["legup"].ConnectionString;
string queue = "SendQueue";
SqlDependency.Stop(str_con);
if(SqlDependency.Start(str_con))
{
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = cn;
// cmd1.CommandType = CommandType.StoredProcedure;
cmd1.CommandText = "Insert into [legup].[dbo].[legup_comments](project_id,user_id,comments,date) Values (3,1,''" + txt_comments.Text + "'',''" + dtt + "'')";
////cmd1.CommandText = "sp_insert_comments";
cmd1.Notification = null;
//cmd1.Parameters.AddWithValue("@proj_id", 3);
//cmd1.Parameters.AddWithValue("@user_id", 1);
//cmd1.Parameters.AddWithValue("@comments", txt_comments.Text);
//cmd1.Parameters.AddWithValue("@dt", dtt);
SqlDependency dependency = new SqlDependency(cmd1);
dependency.OnChange += new OnChangeEventHandler(OnDataInsert);
cn.Open();
cmd1.ExecuteNonQuery();
txt_comments.Text = String.Empty;
display();
}
else
{
Response.Write("failed to start");
}
}
catch (Exception ex)
{
Response.Write("<br>DFGH:: " + ex.Message);
}
finally
{
// SqlDependency.Stop(str_con);
if (cn != null)
cn.Close();
}
}
void OnDataInsert(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = sender as SqlDependency;
Response.Write("sucess on data insert");
// Notices are only a one shot deal
// so remove the existing one so a new
// one can be added
dependency.OnChange -= new OnChangeEventHandler(OnDataInsert);
// Fire the event
Response.Write("<br> Fire the event");
// if (OnNewMessage != null)
// {
// OnNewMessage();
// }
}
如何访问最近插入的来自其他网络表单的记录
或者如何通过这种方式获得插入查询执行
How can i access the recently inserted record from another web forms
Or how can i get notified on the insert query execution
推荐答案
的通知...
this way...
int indicate = cmd1.ExecuteNonQuery();
if (indicate==0)
{
// fail
Messagebox.Show("Record Insertion failed")
}
else
{
// success
Messagebox.Show("Record Inserted Successfully")
}
快乐编码!
:)
Happy Coding!
:)
这篇关于Sql查询通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文