Access 2003、VS 2010 C#、Windows 窗体应用程序
我认为我遇到了这个问题,因为我的文本框等数据绑定到当前数据,所以问题是如何让绑定源读取新数据而无需关闭并重新启动应用程序?没有错误。我尝试了多种方法,但在应用程序当前运行时它们都没有显示新记录,那么我做错了什么?
我的表单加载看起来像这样..
this.fnDisplayPosition();
bdSource = new BindingSource();
string sql = "SELECT * FROM Table1";
OleDbDataAdapter da = new OleDbDataAdapter(sql, myCon);
DataSet ds = new DataSet();
da.Fill(ds, "Table1");
bdSource.DataSource = ds;
// this.table1BindingSource.AddNew();
txtID.DataBindings.Add("Text", bdSource, "ID");
cBAG.DataBindings.Add("Text", bdSource, "AgeGroup");
cBGender.DataBindings.Add("Text", bdSource, "Gender");
fnDisplayPosition 方法读取那里有多少与数据绑定相关的记录...
this.label7.Text = this.table1BindingSource.Position + 1 + " of " +
this.table1BindingSource.Count;
例如,这是我的导航按钮..
this.table1BindingSource.MoveFirst();
this.fnDisplayPosition();
以下是我的插入方法,我可以添加我从这个网站使用过的新记录......
OleDbDataAdapter da = new OleDbDataAdapter(@"INSERT INTO Table1 (ID, AgeGroup, Gender)VALUES
(@txtID, @cBAG, @cBGender", myCon);
string qry = @"select * from Table1";
string upd = @"INSERT INTO Table1 (ID, AgeGroup, Gender)
VALUES(@txtID, @cBAG, @cBGender)";
myCon.Open();
try
{
da.SelectCommand = new OleDbCommand(qry, myCon);
DataSet ds = new DataSet();
da.Fill(ds, "Table1");
DataTable dt = ds.Tables["Table1"];
DataRow newRow = dt.NewRow();
newRow["ID"] = txtID;
newRow["AgeGroup"] = cBAG;
newRow["Gender"] = cBGender;
dt.Rows.Add(newRow);
OleDbCommand cmd = new OleDbCommand(upd, myCon);
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@AgeGroup", cBAG.Text);
cmd.Parameters.AddWithValue("@Gender", cBGender.Text);
da.InsertCommand = cmd;
da.Update(ds, "Table1");
da.Fill(ds, upd);
} catch(Exception ex) {
Console.WriteLine("Error: " + ex);
} finally {
myCon.Close();
}