如何在此代码中添加多SQL依赖项? [英] how do add multi sql dependency to this code?

查看:41
本文介绍了如何在此代码中添加多SQL依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 中具有 person 表,其字段ID和名称为

i have person table in sql server with fields id and Name

和csharp语句效果很好.

and csharp statement that work well.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SQLNotifications
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        string connectionstring = @"Server=EEPERSIAN-PC\SQLEXPRESS;Database=Chatter;User ID=sa;pwd=1";

        delegate void GridDelegate(DataTable table);

        SqlDependency dep;

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlDependency.Start(connectionstring);

            UpdateGrid();
        }

        private void UpdateGrid()
        {
            string sql = "select Name from dbo.person";

            DataTable dt = new DataTable();

            using (SqlConnection con = new SqlConnection(connectionstring))

            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    dep = new SqlDependency(cmd);

                    dep.OnChange += dep_OnChange;

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        dt.Load(rdr);
                    }
                }
            }

            dataGridView1.Invoke(
                (GridDelegate)delegate(DataTable table)
                {
                    dataGridView1.DataSource = table; 
                }
                , dt);
        }
        private void dep_OnChange(object sender, SqlNotificationEventArgs e)
        {
            MessageBox.Show("Insert Accourd");
            UpdateGrid();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            SqlDependency.Stop(connectionstring);
        }
    }
}

现在,我有2个问题:

1-我不能理解这段代码

 dataGridView1.Invoke(
                (GridDelegate)delegate(DataTable table)
                {
                    dataGridView1.DataSource = table; 
                }
                , dt);

2-我要监视多表而不是一个表. 我该怎么做?

请帮助我解决这个问题.

Please help me to solve this problem.

推荐答案

  1. 委托只是一种不返回任何内容的方法.这段代码只是对内联方法的调用.

这样想:

 MyDelegateFunction(dt);

 private void MyDelegateFunction(DataTable table)
 {
    dataGridView1.DataSource = table; 
 }

dt是传递给方法的参数,它设置为dataGridView

dt is the parameter passed into the method, it is set to the datasource of the dataGridView

这是 MSDN 该方法的文章.

2-如果通过监视许多表来表示您希望查看它们中的数据,则只需更改要查询的sql.

2 - If by monitoring many tables you mean you want to see the data from them, then you just change the sql that you are using to query.

此:

string sql = "select Name from dbo.person";

可能会对此进行更改:

string sql = "select 
                 name, other, data 
              from 
                dbo.person p 
                inner join dbo.anothertable a on p.personid = a.personid"

经过一番澄清后,您想要设置插入触发器在您的桌子上对其进行监控.我建议是这样的:

After a bit of clarification, you want to set up insert triggers on your tables to monitor them. I'd suggest something like this:

  1. 发生插入,并且触发OnInsert触发器.
  2. 插入触发器将日志记录信息放入日志记录表.
  3. 您的c#应用程序轮询该表以获取新信息,并相应地执行.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆