更新查询有效,但列表框未显示更新/更改

时间:2013-12-25 20:10:51

标签: c# sql listbox

我创建了一个Windows窗体,您可以在其中搜索名称的电影。然后你可以更新记录。 但是当我尝试这样做时,我得到了保存更新的消息框,但是当我重新加载列表框时,我看不到任何更改。

更新方法。

public void Update()
    {
        OleDbConnection conn = new OleDbConnection(connectionString);

        OleDbCommand command = new OleDbCommand();
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        conn.Open();


        string commandostring = "UPDATE Movies SET " +
                " name = '" + this.name+ "'," +

                " releaseDate = #" + this.releaseDate + "#, " +
                " lenghtInMinutes = " + this.lenghtInMinutes +
                " WHERE movieId = " + this.movieId;


        command.CommandText = commandostring;
        command.Connection = conn;
        adapter.UpdateCommand = command;


        adapter.UpdateCommand.ExecuteNonQuery();


        conn.Close();
    }

Windows窗体更新:

  private void buttonUpdate_Click(object sender, EventArgs e)
    {
        try
        {

            private Movie Movie1;
            Movie1.Name= textBoxName.Text;
            Movie1.ReleaseDate = Convert.ToDateTime(textBoxReleaseDate.Text);
            Movie1.LenghtInMinutes= Convert.ToInt32(textBoxLenghtInMinutes.Text);

            Movie1.Update();
            MessageBox.Show("Changes saved");

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

        }
    }

填写列表框

private void fillListBox()
    {

        List<Movie> movies = Movie.AllMovies();
        listBox1.DataSource = movies;
        listBox1.DisplayMember = "AllData";
        listBox1.ValueMember = "MovieId";


    }

所有电影方法

public static List<Movie> AllMovies()
    {


        string command;
        command = "SELECT movieId, name, headActor, director, actorId, directorId, releaseDate, lenghtInMinutes "
            + "FROM Movies order by movieId";
        OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionString);
        DataTable datatable = new DataTable();
        adapter.Fill(datatable);


        List<Movie> list = new List<Movie>();
        for (int i = 0; i < datatable.Rows.Count; i++)
        {
            Movie movie = new Movie();
            movie.movieId = datatable.Rows[i].Field<int>("movieId");
            movie.name = datatable.Rows[i].Field<string>("name");
            movie.headActor = datatable.Rows[i].Field<string>("headActor");
            movie.director = datatable.Rows[i].Field<string>("director");
            movie.actorId = datatable.Rows[i].Field<int?>("actorId");
            movie.directorId = datatable.Rows[i].Field<int?>("directorId");
            movie.releaseDate = datatable.Rows[i].Field<DateTime>("releaseDate");
            movie.lenghtInMinutes = datatable.Rows[i].Field<int>("lenghtInMinutes");
            list.Add(movie);






        }
        return lijst;
    }

1 个答案:

答案 0 :(得分:0)

要更新数据库,我会使用参数化查询

public void Update()
{
    string commandostring = "UPDATE Movies SET " +
                            "name = ?,releaseDate = ?,lenghtInMinutes = ? " +
                            "WHERE movieId = ?";
    using(OleDbConnection conn = new OleDbConnection(connectionString))
    using(OleDbCommand command = new OleDbCommand(commandostring, conn))
    {
         conn.Open();
         command.Parameters.AddWithValue("@p1",this.name);
         command.Parameters.AddWithValue("@p2",this.releaseDate);
         command.Parameters.AddWithValue("@p3",this.lenghtInMinutes);
         command.Parameters.AddWithValue("@p4",this.movieId);
         command.ExecuteNonQuery();
    }
}

然后,关于列表框更新,查看您之前的问题,似乎填充了列表框,为其数据源分配List<Movie>。要更新列表框,您应该找到要更改的项目(通常是SelectedItem),然后手动更改存储在SelectedItem中的Movie实例并使用它来更新数据库

private void buttonUpdate_Click(object sender, EventArgs e)
{
    try
    {
        // Here we need to get the current movie from the current item 
        // selected in the listbox and update this instance
        if(listBoxForMovies.SelectedItem != null) 
        {
             Movie currentMovie = listBoxForMovies.SelectedItem as Movie;
             currentMovie.Name= textBoxName.Text;
             currentMovie.ReleaseDate = Convert.ToDateTime(textBoxReleaseDate.Text);
             currentMovie.LenghtInMinutes= Convert.ToInt32(textBoxLenghtInMinutes.Text);
             currentMovie.Update();
             MessageBox.Show("Changes saved");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);

    }
}

要解决两个表单之间通信的常见问题,您可以应用这个简单的技术。

在FormSearchMovies中

public class FormSearchMovies:Form
{
    private ListBox listBoxForMovies = null;

    public void FormSearchMovice(ListBox listFromParentForm)
    {
        listBoxForMovies = listFromParentForm;
        InitializeComponent();
    }
}

在FormListMovies

using(FormSearchMovies frmSearch = new FormSearchMovies(listbox1))
{
     frmSearch.ShowDialog();
}