2013-04-08 69 views
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(); 
    } 
+0

要么我的代码是绝对垃圾或没有人知道 – bucketblast 2013-04-09 09:37:40

回答

0

很多试错后,我发现了什么为什么发生。 tableadapters和绑定到其他文本框的绑定源的使用不是要走的路。你必须硬编码。因此,要查看您的数据反映是否更新,删除或添加新记录而不关闭并重新声明应用程序,则不能使用tableadapters或binding source。为了清楚地理解我在说什么,你是否这个网站,here。不管你使用什么连接或你如何使用命令参数。无论如何,这只是我的看法。