使用Sql自动刷新DataGridview [英] Auto refresh DataGridview using Sql

查看:88
本文介绍了使用Sql自动刷新DataGridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是在Visual Studio中制作Winforms的新手,所以我不确定我是否在正确的位置发布了这个。 我创建了一个附有gridview的winform。 此gridview连接到SQL表。 我需要
解决的问题是,如果我有多个人同时使用该应用程序,我需要能够让所有用户自动查看对sql表所做的任何更改,而无需以某种方式刷新屏幕 这有点像聊天程序。 
有人发出聊天消息,每个人都看到它。 这可能吗?

First, I am new at making Winforms in Visual Studio so I am not sure if I am posting this in the right place.  I have created a winform that has a gridview attached to it.  This gridview is connected to a SQL table.  The problem I need to resolve is if I have multiple people using the application at the same time, I need the ability for all users to see any changes made to the sql table automatically without having to refresh the screen in some way.  It kind of like a chat program.  Someone puts in a chat message and everyone sees it.  Is this possible?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dapper;

namespace PositionLogin
{    
    public partial class Form1 : Form
    {
        SqlConnection sqlCon = new SqlConnection(@"Data Source=UCPDAPPS2;Initial Catalog=Dispatch_PositionDB;Integrated Security= True;");
        int PositionId = 0;

        
        public Form1()
        {
            InitializeComponent();
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                    sqlCon.Open();
                DynamicParameters param = new DynamicParameters();
                param.Add("@PositionID", PositionId);
                param.Add("@Position", cBxPosition.Text.Trim());
                param.Add("@Name", cBxName.Text.Trim());
                param.Add("@Ext", cBxExtension.Text.Trim());
                param.Add("@Status", cBxStatus.Text.Trim());

                sqlCon.Execute("Position_LoginAddOrEdit", param, commandType: CommandType.StoredProcedure);
                if (PositionId == 0)
                    MessageBox.Show("Saved Successfully");
                else
                    MessageBox.Show("Updated Successfully");
                FillDataGridView();
                Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
            finally {

                sqlCon.Close();
            }
        }

        void FillDataGridView()
        {
            DynamicParameters param = new DynamicParameters();
            param.Add("@SearchText", txbsearch.Text.Trim());

            List<Contact> list = sqlCon.Query<Contact>("Position_LoginViewAllOrSearch", param, commandType: CommandType.StoredProcedure).ToList<Contact>();

            dataGridView1.DataSource = list;

            dataGridView1.Columns[0].Visible = false;

        }

        class Contact {
            public int PositionID { get; set; }
            public string Position { get; set; }
            public string Name { get; set; }
            public string Ext { get; set; }
            public string Status { get; set; }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                FillDataGridView();
                Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            try
            {
                FillDataGridView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            Clear();
        }

        void Clear()
        {
            cBxExtension.Text = cBxName.Text = cBxPosition.Text = cBxStatus.Text = "";
            PositionId = 0;
            btnsave.Text = "Save";
            btndelete.Enabled = false;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.CurrentRow.Index !=-1)
                {
                PositionId = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                cBxExtension.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
                cBxName.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                cBxPosition.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                cBxStatus.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
                btndelete.Enabled = true;
                btnsave.Text = "Save Edit";

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            try
            {
                DynamicParameters param = new DynamicParameters();
                param.Add("@PositionID", PositionId);
                sqlCon.Execute("Position_LoginDeleteByID",param,commandType:CommandType.StoredProcedure);
                Clear();
                FillDataGridView();
                MessageBox.Show("Deleted Successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txbsearch_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void cBxStatus_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cBxPosition_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cBxName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Status_Click(object sender, EventArgs e)
        {

        }

        private void cBxExtension_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}




推荐答案

嗨wgraulich,

Hi wgraulich,

我建议您考虑使用消息队列机制,当其中一个客户端修改数据时,此客户端向消息中间件发送消息,然后中间件向所有其他客户端发送消息以更新
数据:

I suggest you can consider using the message queue mechanism, when one of the clients modifies the data, then this client sends a message to the message middleware, then the middleware sends a message to all other clients to update the data:

有关消息队列的更多详细信息,请参阅以下文档:

For more details about message queue, please refer to the following documents:

DotNetMQ:一个完整​​的消息队列系统.NET。

关于消息和消息队列。

使用消息和消息队列。

希望这会有所帮助!

问候,

Stanly


这篇关于使用Sql自动刷新DataGridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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