0

我正在创建一个具有 Sql 查询通知的应用程序,但我遇到了一个问题.. 系统一直在循环,进入无限循环。我使用的查询在存储过程中,我使用了 7我的查询中的表。我的查询或我的应用程序似乎有什么问题?请帮我解决这个问题谢谢:)我会在这里发布我的代码这是我正在使用的存储过程

ALTER PROCEDURE [dbo].[INVENTORYLOG_VIEW]
AS
    BEGIN
    SELECT     
     ACCOUNT.Emp_Fname + ' ' + ACCOUNT.Emp_Lname AS [USERS],
     INVENTORYLOG.ACTIVITYDATE AS [ACTIVITY DATE], 
     INVENTORYLOGDETAILS.ACTIVITYTIME AS [ACTIVITY TIME],
     INVENTORYLOGDETAILS.LOG_ACTION AS [USER LOG], 
     CATEGORY.CATEGORY_NAME, 
     BRAND.Brand_Name AS BRAND, 
     PRODUCTDESCRIPTION.DESCRIPTION_TYPE, 
     COLOR.Color_Name AS [COLOR NAME], 
     SIZES.SIZE, 
     INVENTORY.QUANTITY_ON_HAND AS [CURRENT QTY], 
     INVENTORYLOGDETAILS.QUANTITY_LOG_ACTION AS [USER LOG QTY]
FROM         
dbo.ACCOUNT INNER JOIN
dbo.INVENTORYLOG ON dbo.ACCOUNT.ACCOUNT_ID = dbo.INVENTORYLOG.Account_Id INNER JOIN
dbo.INVENTORYLOGDETAILS ON dbo.INVENTORYLOG.INVENTORYLOGID = dbo.INVENTORYLOGDETAILS.INVENTORYLOGID INNER JOIN
dbo.INVENTORY ON dbo.INVENTORYLOGDETAILS.INVENTORY_ID = dbo.INVENTORY.INVENTORY_ID INNER JOIN
dbo.PRODUCT_DTL ON dbo.INVENTORY.PRODUCT_DTL_ID = dbo.PRODUCT_DTL.PRODUCT_DTL_ID INNER JOIN
dbo.COLOR ON dbo.PRODUCT_DTL.COLOR_ID = dbo.COLOR.Color_Id INNER JOIN
dbo.BRAND ON dbo.PRODUCT_DTL.BRAND_ID = dbo.BRAND.Brand_Id INNER JOIN
dbo.CATEGORY ON dbo.PRODUCT_DTL.CATEGORY_ID = dbo.CATEGORY.CATEGORY_ID INNER JOIN
dbo.PRODUCTDESCRIPTION ON dbo.PRODUCT_DTL.DESCRIPTION_ID = dbo.PRODUCTDESCRIPTION.DESCRIPTION_ID INNER JOIN
dbo.SIZES ON dbo.PRODUCT_DTL.SIZE_ID = dbo.SIZES.SIZE_ID
END

这是我的应用程序中的代码

        string connect;
        String connection;
        private SqlConnection cons = null;
        private SqlCommand command = null;
        private DataSet myDataSet = null;
        public Inventory_InventoryLog()
        {
            InitializeComponent();
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            ConnectionString access = new ConnectionString();
            connection = access.AccessConnection(connect).ToString();

            // Sql Notifications Automatic
            EnoughPermission();
        }

        public bool EnoughPermission()
        {
            SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);
            try
            {
                perm.Demand();
                MessageBox.Show(""+perm);
                return true;
            }
            catch(System.Exception)
            {
                return false;
            }
        }
        private void GetActivateData()
        {
            try
            {
                //String query = "SELECT [USER], [ACTIVITY DATE], [ACTIVITY TIME], [USER LOG], CATEGORY_NAME, Brand_Name, DESCRIPTION_TYPE, Color_Name, SIZE, [ CURRENT QTY], [USER LOG QTY] FROM INVENTORY_INVENTORY_LOG_DETAILS";
                SqlDependency.Stop(connection);
                SqlDependency.Start(connection);
                if (cons == null)
                  cons = new SqlConnection(connection);
                if (command == null)
                {
                    command = new SqlCommand("dbo.INVENTORYLOG_VIEW", cons);
                    command.CommandType = CommandType.StoredProcedure;
                }
                if (myDataSet == null)
                    myDataSet = new DataSet();
                GetActualData();
            }
            catch (Exception p)
            {
                MessageBox.Show(""+p);
            }
            MessageBox.Show("Being called");
        }
        private void GetActualData()
        {
            myDataSet.Clear();
            command.Notification = null;
            SqlDependency dependecy = new SqlDependency(command);
            dependecy.OnChange +=new OnChangeEventHandler(dependecy_OnChange);
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                adapter.Fill(myDataSet, "dbo.ACCOUNT, dbo.INVENTORYLOG, dbo.INVENTORYLOGDETAILS, dbo.INVENTORY, dbo.PRODUCT_DTL,dbo.COLOR,dbo.BRAND,dbo.CATEGORY,dbo.PRODUCTDESCRIPTION, dbo.SIZES");
                gridControl1.DataSource = myDataSet.Tables["dbo.ACCOUNT, dbo.INVENTORYLOG, dbo.INVENTORYLOGDETAILS, dbo.INVENTORY, dbo.PRODUCT_DTL,dbo.COLOR,dbo.BRAND,dbo.CATEGORY,dbo.PRODUCTDESCRIPTION, dbo.SIZES"];
                gridView1.BestFitColumns();
                gridView1.BestFitMaxRowCount = 10;
            }

        }

        delegate void UIDelegate();
        public void dependecy_OnChange(object sender, SqlNotificationEventArgs e)
        {
            try
            {
                UIDelegate uidel = new UIDelegate(RefreshDatas);
                this.Invoke(uidel, null);
                SqlDependency dependency = (SqlDependency)sender;
                dependency.OnChange -= dependecy_OnChange;
            }
            catch (Exception es)
            {
             //   XtraMessageBox.Show(es.Message);
            }
        }
        public void RefreshDatas()
        { 
            ////////////////////////
            //---------------------------------------   NOTIFICATION INSERTS HERE ----------------------------------- //////
            label1.Text = "Changes Happened";
            GetActualData();
        }

        /// <summary>
        /// //////////////////////////////////////////////////////////////////////////////////////////////////
        /// //--------------------------------------------------------  SQL NOTIFICATION ------------------------------------
        /// </summary>
        /// <param name="args"></param>


        public SqlConnection ConnectionStringMethod()
        {
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            return con;
        }
        private void Inventory_InventoryLog_Load(object sender, EventArgs e)
        {
            GetActivateData();           
        }

我真的需要这些问题的答案,请帮帮我...谢谢:)

4

1 回答 1

1

您在 RefreshDatas() 中调用 GetActualData(),而后者又由在 GetActiveData() 中触发的 onchange 委托调用。

GetActiveData() 再次调用 GetActualData(),因此循环。

要“修复”它,您应该从 GetActualData 中删除 onchange 委托并将其移至 GetActivateData。

像这样:

private void GetActivateData()
        {
            try
            {
                //String query = "SELECT [USER], [ACTIVITY DATE], [ACTIVITY TIME], [USER LOG], CATEGORY_NAME, Brand_Name, DESCRIPTION_TYPE, Color_Name, SIZE, [ CURRENT QTY], [USER LOG QTY] FROM INVENTORY_INVENTORY_LOG_DETAILS";
                SqlDependency.Stop(connection);
                SqlDependency.Start(connection);
                if (cons == null)
                  cons = new SqlConnection(connection);
                if (command == null)
                {
                    command = new SqlCommand("dbo.INVENTORYLOG_VIEW", cons);
                    command.CommandType = CommandType.StoredProcedure;
                }
                if (myDataSet == null)
                    myDataSet = new DataSet();

            SqlDependency dependecy = new SqlDependency(command);
            dependecy.OnChange +=new OnChangeEventHandler(dependecy_OnChange);


                GetActualData();
            }
            catch (Exception p)
            {
                MessageBox.Show(""+p);
            }
            MessageBox.Show("Being called");
        }
        private void GetActualData()
        {
            myDataSet.Clear();
            command.Notification = null;
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                adapter.Fill(myDataSet, "dbo.ACCOUNT, dbo.INVENTORYLOG, dbo.INVENTORYLOGDETAILS, dbo.INVENTORY, dbo.PRODUCT_DTL,dbo.COLOR,dbo.BRAND,dbo.CATEGORY,dbo.PRODUCTDESCRIPTION, dbo.SIZES");
                gridControl1.DataSource = myDataSet.Tables["dbo.ACCOUNT, dbo.INVENTORYLOG, dbo.INVENTORYLOGDETAILS, dbo.INVENTORY, dbo.PRODUCT_DTL,dbo.COLOR,dbo.BRAND,dbo.CATEGORY,dbo.PRODUCTDESCRIPTION, dbo.SIZES"];
                gridView1.BestFitColumns();
                gridView1.BestFitMaxRowCount = 10;
            }

        }
于 2013-04-07T08:31:17.813 回答