2 lichubo lichubo 于 2016.04.08 09:54 提问

sqldependency 无法触发OnChange时间 8C

class Program
{
private static string _connStr;

    static void Main(string[] args)
    {
        _connStr = "Data Source=.;user ID=sa;Pwd=123456;Database=A;";
        SqlDependency.Start(_connStr);  
        UpdateGrid();

        Console.Read();
    }


    private static void UpdateGrid()
    {
        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            using (SqlCommand command = new SqlCommand("select ID,UserID,[Message] From [dbo].[aa]", connection))
            {
                command.CommandType = CommandType.Text;
                connection.Open();
                SqlDependency dependency = new SqlDependency(command);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                SqlDataReader sdr = command.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();
            }
        }
    }


    private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        UpdateGrid();
    }
            发现在数据库A中能触发事件,在库B中无法触发事件
            Select  DATABASEpRoPERTYEX('a','IsBrokerEnabled')//1

Select DATABASEpRoPERTYEX('B','IsBrokerEnabled')//1

1个回答

caozhy
caozhy   Ds   Rxr 2016.04.08 15:14
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!