在C#winforms中对Sql查询通知中的无限循环进行故障排除

时间:2013-04-07 08:24:53

标签: c# sql

我正在创建一个具有Sql查询通知的应用程序,但我遇到了一个问题..系统保持循环,进入无限循环。我使用的查询是在存储过程中,我' ved在我的查询中使用了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();           
        }

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

1 个答案:

答案 0 :(得分: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;
            }

        }