在mysql c#中插入数据后刷新组合框

时间:2014-03-15 08:44:12

标签: c# mysql combobox

您好我想在我添加或删除数据之后刷新我的组合框现在如果我添加数据它不会刷新我必须重新运行程序以查看更改但我想在我添加时刷新它数据..

添加数据时的代码:

private void button5_Click(object sender, EventArgs e)
    {
        MySqlConnection dataConnection = new MySqlConnection();
        dataConnection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
        dataConnection.Open();
        MySqlTransaction transakcija = dataConnection.BeginTransaction();
        MySqlCommand dataCommand = new MySqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.Transaction = transakcija;
        try
        {
            dataCommand.CommandText = "Insert INTO filmi.film (film) VALUES ('" + this.tB_Dodaj.Text + "')";
            dataCommand.CommandType = CommandType.Text;
            dataCommand.ExecuteNonQuery();
            transakcija.Commit();
            MessageBox.Show("You added a new movie!");
        }
        catch (Exception eks)
        {
            transakcija.Rollback();
            MessageBox.Show("Movie couldnt be added!!\n" + eks.Message);
        }
        finally
        {
            dataCommand.Connection.Close();
        }
    }

并且每次插入时,数据都会显示在组合框中,但只有在我重新运行程序时才会显示

这就是我填充组合框的方式:

void Fillcombo()
    {
        string constring = "datasource=localhost;port=3306;username=root;password=";
        string Query = "SELECT * FROM filmi.film ;";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                string sName = myReader.GetString("film");
                comboBox1.Items.Add(sName);
                comboBox2.Items.Add(sName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

2 个答案:

答案 0 :(得分:1)

  

我必须重新运行该程序以查看更改,但我想得到它   刷新我添加数据的时间..

我怀疑您在Form_Load事件处理程序中调用Fillcombo()方法。

如果要为表中的每个插入和删除操作更新组合框,则需要在执行命令后立即调用Fillcombo()

试试这个:

int status = dataCommand.ExecuteNonQuery();
transakcija.Commit();
if(status > 0)
{
    Fillcombo();
    MessageBox.Show("You added a new movie!");
}

在您的FillCombo中清除项目,然后添加新项目以删除重复项。

comboBox1.Items.Clear(); //add this statetement before adding items
comboBox2.Items.Clear(); //add this statetement before adding items
while (myReader.Read())
{
    string sName = myReader.GetString("film");
    comboBox1.Items.Add(sName);
    comboBox2.Items.Add(sName);
}

答案 1 :(得分:0)

ADO.NET对象在断开状态下工作,因此,向数据表中添加记录不会自动显示在组合中。你需要再次调用FillCombo,(清除已经在组合中的项目,再次访问数据库以再次检索每个记录,将它们添加到组合框中)或者只是将电影添加到已经填充的组合中作为单个项目

还要注意Sql Injection(总是使用参数化查询)

private void button5_Click(object sender, EventArgs e)
{
    string conString = "datasource=localhost;port=3306;username=root;password=";
    string cmdText = "Insert INTO filmi.film (film) VALUES (@film)";
    using(MySqlConnection dataConnection = new MySqlConnection(conString))
    using(MySqlCommand dataCommand = new MySqlCommand(cmdText, dataConnection))
    {
         try
         {
            dataConnection.Open();
            dataCommand.Parameters.AddWithValue("@film", this.tB_Dodaj.Text);

            // If ExecuteNonQuery returns a value > 0 then your record has been inserted
            // Just add the name of the film to the two combos 
            if(dataCommand.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("You added a new movie!");
                comboBox1.Items.Add(this.tB_Dodaj.Text);
                comboBox2.Items.Add(this.tB_Dodaj.Text);
            }
         }
         catch(Exception ex)
         {
            MessageBox.Show("Fail to add a new movie! " + ex.Message);
         }
    }
}

作为最后一点,请仔细观察您的fillcombo方法。您不要关闭并处置连接。