在向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());
}
}
}
答案 0 :(得分:2)
您的BindingSource是否使用DataSet?如果是这样,您需要通过DataSet进行插入,并且可能刷新绑定源。如果你这样做,你也可以避免重复的插入逻辑。
或者,你可以刷新DataSet,但是这种方法没有利用DataSet的功能,并且会导致很多重复的代码。
答案 1 :(得分:0)
我最近在VB.NET中遇到了同样的问题。我发现将数据设置为Nothing(我相信C#中为null),然后将其分配给我的对象列表解决了问题。
即
attendanceDataFiles.DataSource = Nothing
attendanceDataFiles.DataSource = myFileList.files