【问题标题】:How do i refresh my listbox with the new data?如何用新数据刷新我的列表框?
【发布时间】:2017-11-23 13:05:35
【问题描述】:

我有一个显示数据库中所有行的列表框。 当我从列表框/数据库中编辑一行时,编辑不会显示在列表框中,直到我重新启动程序。单击“保存更改”按钮后,如何修复以使列表框随编辑更新?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Library;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            var query = "SELECT * FROM Author";
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Author a = new Author();
                        a.Id = reader.GetInt32(0);
                        a.Name = reader.GetString(1);
                        a.Nationality = reader.GetString(2);

                        AuthorListBox.Items.Add(a.Name);

                        Book b = new Book();
                        b.BookId = reader.GetInt32(0);
                        //b.AuthorID = reader.GetInt32(1);
                        b.Title = reader.GetString(2);

                        BookListBox.Items.Add(b.BookId);
                    }
                }
            }
        }
    }

    private void SaveChangesButton_Click(object sender, RoutedEventArgs e)
    {
        string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Library;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            try
            {
                connection.Open();
                string query = "UPDATE Author SET AuthorId = '" + this.IdTextBox.Text + "' ," +
                    " Name = '" + this.AuthorNameTextBox.Text + "', Nationality = '" + this.NationalityTextBox.Text +
                    "' WHERE AuthorId = '" + this.IdTextBox.Text + "' ";
                SqlCommand command = new SqlCommand(query, connection);
                command.ExecuteNonQuery();
                MessageBox.Show("Updated the author");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
            }

        }
    }

【问题讨论】:

    标签: c# sql database wpf listbox


    【解决方案1】:

    对我来说,您似乎没有读回您对数据库所做的更改。您只需将 Author 对象添加到您的 Constructor 中的 BookListBox.Items 集合中。

    最简单的方法可能是在添加元素并更新 BookListBox.Items 集合后再次从数据库中读取所有内容。顺便说一下,不要忘记调用 refresh()。

    【讨论】:

    • 谢谢,我会调查的。我应该在command.ExecuteNonQuery(); 之后调用 refresh() 吗?
    • 在将所有新元素添加到您的 VookListBox.Items 集合后调用它。
    【解决方案2】:

    一种方法是将检索数据库值的代码放入方法中,例如

    public void getData(){

    }

    那么当你更新作者时,在 MessageBox.Show("Updated the author"); 之后立即调用这个方法

    试试吧,如果不行可以给你另一个角度。

    还将您的连接字符串放在函数/方法中,以避免在打开数据库连接时重复自己

    【讨论】:

      【解决方案3】:

      MainWindow() 的内容提取到不同的函数中。并在SaveChangesButton_Click 末尾调用新函数。另外不要忘记在刷新内容之前清除列表框。

      类似的东西

      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              LoadFromDatabase();
          }
          public LoadFromDatabase()
          {
      
              BookListBox.Items.Clear(); //Without this you'll get an ever expanding list
              string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Library;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
              using (SqlConnection con = new SqlConnection(connectionString))
              {
                  con.Open();
                  var query = "SELECT * FROM Author";
                  using (SqlCommand cmd = new SqlCommand(query, con))
                  {
                      using (SqlDataReader reader = cmd.ExecuteReader())
                      {
                          while (reader.Read())
                          {
                              Author a = new Author();
                              a.Id = reader.GetInt32(0);
                              a.Name = reader.GetString(1);
                              a.Nationality = reader.GetString(2);
      
                              AuthorListBox.Items.Add(a.Name);
      
                              Book b = new Book();
                              b.BookId = reader.GetInt32(0);
                              //b.AuthorID = reader.GetInt32(1);
                              b.Title = reader.GetString(2);
      
                              BookListBox.Items.Add(b.BookId);
                           }
                       }
                   }
               }
           }
      
          private void SaveChangesButton_Click(object sender, RoutedEventArgs e)
          {
              string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Library;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
              using (SqlConnection connection = new SqlConnection(connectionString))
              {
                  try
                  {
                      connection.Open();
                      string query = "UPDATE Author SET AuthorId = '" + this.IdTextBox.Text + "' ," +
                      " Name = '" + this.AuthorNameTextBox.Text + "', Nationality = '" + this.NationalityTextBox.Text +
                      "' WHERE AuthorId = '" + this.IdTextBox.Text + "' ";
                      SqlCommand command = new SqlCommand(query, connection);
                      command.ExecuteNonQuery();
                      MessageBox.Show("Updated the author");
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
                  finally
                  {
                      connection.Close();
                  }
      
              }
              LoadFromDatabase();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-07-11
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 2013-11-23
        • 2016-03-09
        相关资源
        最近更新 更多