C#winform里如何使用Datagirdview控件将SQLserver中的某个数据实时显示出来?

img


如图,想要制作如图所示的效果,连接数据库在控件中实时显示某个具体数据,请问用什么控件比较好,具体如何实施

在 C# Winform 程序中,可以通过以下步骤使用 DataGridView 控件实时显示 SQL Server 中的数据:

  1. 添加 DataGridView 控件
    在 Windows 窗体中拖拽一个 DataGridView 控件,设置其 Dock 属性为 Fill 以填满整个窗体。
  2. 连接 SQL Server 数据库
    使用 SqlConnection 类连接 SQL Server 数据库。
csharp
string connStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();

  1. 执行查询语句
    使用 SqlCommand 执行查询语句,查询所需数据。
csharp 
string sql = "SELECT * FROM table1";
SqlCommand cmd = new SqlCommand(sql, conn);

  1. 获取查询结果
    使用 SqlDataReader 读取查询结果。
csharp
SqlDataReader dr = cmd.ExecuteReader();

  1. 填充 DataGridView
    遍历读取到的每一行数据,将值添加到 DataGridView 的行中。
csharp
while (dr.Read())
{
    DataGridViewRow row = new DataGridViewRow();
    row.CreateCells(dataGridView1);
    row.Cells[0].Value = dr["id"];
    row.Cells[1].Value = dr["name"];
    // ...
    dataGridView1.Rows.Add(row);
}

  1. 实时更新
    通过 Timer 定时器周期性执行步骤 3 - 5,即可实现实时从数据库加载数据并显示在 DataGridView 中。
csharp 
Timer timer = new Timer();
timer.Interval = 1000; // 1 秒
timer.Tick += (s, e) =>
{
    // 执行查询并填充 DataGridView  
};
timer.Start();

这样,DataGridView 就可以每隔 1 秒自动去数据库查询最新数据并更新显示,实现实时显示数据的效果。

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/766654
  • 这篇博客你也可以参考下:WinForm多线程+滚动条事件实现Datagirdview定时刷新表数据完整源码附注释
  • 以下回答由chatgpt基于相关博客总结生成:

    对于如何实现和使用DataGridView控件实时显示SQL Server数据库中特定数据的问题,可以按照以下步骤进行操作:

    1. 创建Winform程序界面,并添加DataGridView控件。

    2. 在程序中使用SqlConnection对象连接SQL Server数据库,并编写查询语句。

    3. 用SqlDataAdapter适配器将查询结果填充到DataSet对象中,例如:

    string sql = "SELECT * FROM employee";
    SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
    DataSet dataset = new DataSet();
    adapter.Fill(dataset, "employee");
    
    1. 将DataSet中的表作为DataGridView的数据源,例如:
    dataGridView1.DataSource = dataset.Tables["employee"];
    

    然后在需要实时更新数据的地方,例如点击按钮时,可以重新查询数据库,将结果再次填充到DataSet中,并将DataGridView的数据源指向新的DataSet,以实现实时更新数据的效果。

    1. 修改当前选中行的数据可以按照如下步骤进行操作:

    2. 在form1中编写代码,获取当前选中的行的ID,然后创建并显示form2。例如:

    if (this.dataGridView1.SelectedRows.Count > 0)
    {
        int index = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value.ToString());
        ChangeForm changeForm = new ChangeForm(index.ToString());
        changeForm.ShowDialog();
    }
    else
    {
        MessageBox.Show("请先选中再操作");
    }
    
    • 在form2中通过构造函数传入选中行的ID,然后根据ID查询数据库,显示在文本框中。例如:
    public ChangeForm(string id)
    {
        InitializeComponent();
    
        string sql = "SELECT * FROM employee WHERE id=@id";
        SqlCommand sqlCommand = new SqlCommand(sql, connection);
        sqlCommand.Parameters.AddWithValue("@id", id);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        reader.Read();
        textBox1.Text = reader["name"].ToString();
        textBox2.Text = reader["age"].ToString();
        textBox3.Text = reader["sex"].ToString();
        reader.Close();
    }
    
    • 在form2中修改文本框中的数据并保存回数据库。例如:
    private void button1_Click(object sender, EventArgs e)
    {
        string sql = "UPDATE employee SET name=@name, age=@age, sex=@sex WHERE id=@id";
        SqlCommand sqlCommand = new SqlCommand(sql, connection);
        sqlCommand.Parameters.AddWithValue("@name", textBox1.Text);
        sqlCommand.Parameters.AddWithValue("@age", textBox2.Text);
        sqlCommand.Parameters.AddWithValue("@sex", textBox3.Text);
        sqlCommand.Parameters.AddWithValue("@id", id);
        int result = sqlCommand.ExecuteNonQuery();
        if (result > 0)
        {
            MessageBox.Show("修改成功!");
        }
    }
    

    以上是关于如何实现和使用DataGridView控件展示SQL Server数据库中特定数据以及修改数据的具体指导。