在我的本地数据库中,Sql Dependency工作正常,但是当我迁移到Azure数据库时无法正常工作。
我检查服务代理是否已启用并已激活。
这是错误:
此版本的SQL Server不支持语句“ RECEIVE MSG”
这是我的代码:
public class Program
{
static void Main(string[] args)
{
SqlClientPermission permission = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);
if (SolicitarNotifications())
{
string con = "Server=tcp:xxxxx.database.windows.net,0000;Initial Catalog=TestSQLDependendcy;Persist Security Info=False;User ID=xxxx;Password=xxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
SqlConnection conn = new SqlConnection(con);
SqlDependency.Start(con);
Console.WriteLine("Empezando a escuchar");
GetAlerts();
Console.WriteLine("Presiona enter para salir");
Console.ReadLine();
Console.WriteLine("Deteniendo la escucha...");
SqlDependency.Stop(con);
}
else {
Console.WriteLine("No tienes permitido solicitar notificaciones");
}
}
public static void GetAlerts()
{
try
{
using (SqlConnection con = new SqlConnection("Server=tcp:xxxxx.database.windows.net,0000;Initial Catalog=TestSQLDependendcy;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"))
{
SqlCommand cmd = new SqlCommand("Select Correo, Nombre From TestNombre", con);
cmd.CommandType = CommandType.Text;
cmd.Notification = null;
cmd.Dispose();
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += new OnChangeEventHandler(OnDataChange);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
{
while (dr.Read())
{
if (dr.HasRows)
{
using (MailMessage mm = new MailMessage())
{
//Send Mails
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
private static void OnDataChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= new OnChangeEventHandler(OnDataChange);
GetAlerts();
}
}
如何在Azure SQL数据库中运行此服务?
当前,Azure SQL不支持Service Broker。
在https://docs.microsoft.com/zh-cn/sql/t-sql/statements/receive-transact-sql?view=sql-server-2017上找到RECEIVE语句支持的版本,该声明指出Azure SQL不支持它。 。
此外,该文档https://docs.microsoft.com/zh-cn/azure/sql-database/sql-database-features提供了SQL Server,Azure SQL和托管实例之间的出色功能比较文档。请注意,管理实例在某些方面支持Service Broker。我建议注册并尝试一下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句