0
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();
以下是我的插入方法,我可以添加新纪录我从this网站上使用...
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();
}
要么我的代码是绝对垃圾或没有人知道 – bucketblast 2013-04-09 09:37:40