事件发生时从SQL Server数据库通知Winforms应用程序

时间:2019-02-24 19:08:27

标签: c# sql-server linq-to-sql database-trigger check-constraints

我正在使用Linq-to-SQL与SQL Server数据库进行交互。我正在从应用程序的不同部分插入一些表的数据。插入后我有一个触发器可以处理其他一些事情。根据此触发器内部的逻辑,我希望能够通知应用程序。

示例:

我卖2件商品,Pensbooks。所以我insert into table Sales

在这两个插入项上,我都会更新剩余多少项(笔/本)的值。所以我update the some values in table Inventory

现在,如果状态低于某个特定数字,请说10,我想通过一条消息通知应用程序:

The inventory for item {x} is getting low

我想到了两种可能的方法

  1. SqlDependency

    但是我不知道如何将SqlDependencylinq to sql一起使用。

  2. check constraint

    check constraint添加到包含剩余数量的列中。但是check constraint将显示错误。我想收到类似警告的信息。触发器也位于数据库中。如何从触发器获取输出消息?

是否可以从触发器内部调用c#函数?我的意思是一个c#函数与用于插入的代码在同一个项目中?就像我在窗体中有一个只显示MessageBox的方法一样

1 个答案:

答案 0 :(得分:0)

如果您使用的是数据库,并且要不断更新,则可以使用yield函数和while循环创建代码,以使您的代码将消息“项目{x}的库存不足”返回到字符串变量。 例如,如果您使用的是c#代码,则该代码将一直运行(while循环或作为网站的一部分或其他内容),则可以使用如下代码:

        //get an array of strings where the following slot to every product name slot is it's quantity
    public static string[] Getquantity(string fileName, string tableName) 
    {
        string sql = "SELECT * FROM " + tableName;
        string path = HttpContext.Current.Server.MapPath("App_Data/" + fileName);//"App_Data/ is an example for the directory of the database
        string connString = @"provider=Microsoft.ACE.OLEDB.12.0; Data source=" + path;
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString);
        conn.Open();
        System.Data.OleDb.OleDbDataAdapter tableAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, conn);
        System.Data.DataTable dt = new System.Data.DataTable();
        tableAdapter.Fill(dt);
        int size = dt.Columns.Count;
        string[] UserData = new string[size];
        int i = 0;
        foreach (System.Data.DataRow row in dt.Rows)
        {
            foreach (object myItemArray in row.ItemArray)
            {
                UserData[i] = myItemArray.ToString();
                i++;
            }
        }
        return UserData;
    }
    //the alreting funtion
    public static System.Collections.IEnumerator SupplyStatus()
    {
        string status = "";
        string[] array = Getquantity("file name", "sales");
        //if you have any trigger that you'd like to stop the code, you can define that instead of the '0!=1' bool..
        while (0!=1){
            for (int i=0; i > array.Length; i++)
            {
                if (i % 2 == 1) { 
                    if (int.Parse(array[i])<10) 
                    {
                        status = "The inventory for item" + array[i-1] + "is getting low";
                        yield return status;
                    }
                    else {
                        status = "";
                        yield return status;
                    }
                }
            }

        }
        yield break;
    }
    static void Main(string[] args)
    {
        string status = SupplyStatus().ToString();
        while (0 != 1) {
//writing the current status and deleting the last one written
            Console.WriteLine(status);
            Console.SetCursorPosition(0, Console.CursorTop - 1);
            int currentLineCursor = Console.CursorTop;
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.Write(new string(' ', Console.WindowWidth));
            Console.SetCursorPosition(0, currentLineCursor); 
            status = SupplyStatus().ToString();
        }
    }