【问题标题】:Using SQL Dependency with Azure将 SQL 依赖项与 Azure 结合使用
【发布时间】:2018-06-01 18:33:47
【问题描述】:

在我的本地数据库中,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 数据库中运行该服务?

【问题讨论】:

    标签: sql sql-server azure azure-sql-database service-broker


    【解决方案1】:

    目前,Azure SQL 不支持 Service Broker。

    https://docs.microsoft.com/en-us/sql/t-sql/statements/receive-transact-sql?view=sql-server-2017 处查找 RECEIVE 语句支持的版本,其中指出 Azure SQL 不支持它。

    此外,此文档https://docs.microsoft.com/en-us/azure/sql-database/sql-database-features 提供了 SQL Server、Azure SQL 和托管实例之间的出色功能比较文档。请注意,Manage Instance 支持 Service Broker,但存在一些差异。我建议注册预览版并尝试一下。

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2013-02-01
      • 2021-11-03
      相关资源
      最近更新 更多