0

Access データベースに新しいデータを追加した後、ComboBox を更新できないようです。

新しいデータを追加するために使用するコードは次のとおりです。

private void btnAddUser_Click(object sender, EventArgs e)
{
        AccountForm actFrm = new AccountForm();

        if (actFrm.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this.oleDbDataAdapter1.InsertCommand.CommandText =
                    "INSERT INTO userTable (AccountName, Username, PopServer, PopPort, Psswrd, SmtpServer, SmtpPort, Email)" +
                    "VALUES     ('" + actFrm.txtAccName.Text + "','" + actFrm.txtUsername.Text + "','" + actFrm.txtPop3.Text + "','" + actFrm.txtPop3Port.Text + "','" + actFrm.txtPassword.Text + "','" + actFrm.txtSmtp.Text + "','" + actFrm.txtSmtpPort.Text + "','" + actFrm.txtEmail.Text + "')";

                //open the bridge between the application and the datasource
                this.oleDbConnection1.Open();

                this.oleDbDataAdapter1.InsertCommand.Connection = oleDbConnection1;

                //execute the query 
                this.oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();

                //close the connection
                this.oleDbConnection1.Close();

                MessageBox.Show("User Added Successfully");  //inform the user
                //tried here to refresh and even open close the myConn connection. to no avail
            }
            catch (System.Data.OleDb.OleDbException exp)
            {
                //close the connection
                this.oleDbConnection1.Close();

                MessageBox.Show(exp.ToString());
            }
      }
}
4

2 に答える 2

2

BindingSource は DataSet を使用していますか? その場合は、DataSet を介して挿入を行い、場合によってはバインディング ソースを更新する必要があります。このようにすると、挿入ロジックの重複も回避できます。

別の方法として、DataSet を更新することもできますが、この方法では DataSet の機能を利用できず、多くの重複コードが発生します。

于 2009-02-04T17:28:25.190 に答える
0

最近、VB.NET でも同じ問題が発生しました。datasoure を Nothing に設定し (C# では null だと思います)、それをオブジェクトのリストに割り当てると、問題が解決することがわかりました。

すなわち

  attendanceDataFiles.DataSource = Nothing
  attendanceDataFiles.DataSource = myFileList.files
于 2010-04-15T14:32:36.887 に答える