我正在尝试使用signalR监控多个表。这些表是相关的,但我需要所有表中的所有列用于显示目的。我在一张桌子上看过SQldependcy。如何为多个表实现它?这是为多个表实现sqldependecy和signalr的正确方法吗?在数据库中有不同的表,每个表都有ForiegnKey master ---> submaster --->详细信息。请提出建议!
var masterpc = new List<master_Table>();
var submaster = new List<submaster_Table>();
var detail = new List<detail_Table>();
using (SqlConnection connection = new SqlConnection(regularConnectionString))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
//var dependency = new SqlDependency(command);
//dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// NOTE: You have to execute the command, or the notification will never fire.
var reader = command.ExecuteReader();
while (reader.Read())
{
masterpc.Add(item: new master_Table
{
MasterKeyId = (int)reader["MasterKeyId"],
Master_Name = (string)reader["Master_Name"],
Master_IP = reader["Master_IP"] != DBNull.Value ? (string)reader["Master_IP"] : "",
Master_Valid = (bool)reader["Master_Valid"],
});
count++;
}
masterViewModel.masterpc_info = masterpc;
}
}
count = 0;
using (SqlConnection connection = new SqlConnection(regularConnectionString))
{
commandText = "select * from submaster where master_Valid=1 and masterKeyId in(select masterKeyId from masterpc_table where id=24) ";
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
//var dependency = new SqlDependency(command);
//dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// NOTE: You have to execute the command, or the notification will never fire.
var reader = command.ExecuteReader();
while (reader.Read())
{
submaster.Add(item: new submaster_table
{
SubmasterKeyId = (int)reader["SubmasterKeyId"],
submaster_Type = (string)reader["submaster_Type"],
submaster_SN = reader["submaster_SN"] != DBNull.Value ? (string)reader["submaster_SN"] : "",
masterPCKeyId = (int)reader["masterPCKeyId"],
});
count++;
}
masterconfigViewModel.submasterinfo = submaster;
}
}
using (SqlConnection connection = new SqlConnection(regularConnectionString))
{
commandText = "select * from detail where submasterKeyId in(select submasterkeyid from masterpc_table where id=24) ";
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// NOTE: You have to execute the command, or the notification will never fire.
var reader = command.ExecuteReader();
while (reader.Read())
{
detail.Add(item: new detail_table
{
detailkeyid = (int)reader["detailkeyid"],
detail_Type = (string)reader["detail_Type"],
detail_status = reader["detail_status"] != DBNull.Value ? (string)reader["detail_status"] : "",
submasterkeyid = (int)reader["submasterkeyid"],
});
count++;
}
masterconfigViewModel.detailinfo = detail;
}
}
答案 0 :(得分:0)
回答可能有些晚,但您可能会认为以下选项适合您。
项目称为SqlDependencyEx
https://github.com/dyatchenko/ServiceBrokerListener
如何用于多个表
您需要做的就是创建多个不同的侦听器 身份如下所示:
var listener1 = new SqlDependencyEx(connectionString, "YourDatabase", "YourTable1", identity: 1); var listener2 = new SqlDependencyEx(connectionString, "YourDatabase", "YourTable2", identity: 2);