我有一個數據源添加到我的C#項目中,它只有幾列,用戶將輸入數據並向數據庫添加行,並且還能夠搜索特定的行。我無法保存任何更改。當我按下添加按鈕時,我的DataGridView
出現更改,但當我重新啓動我的程序時(我構建並以btw方式導出它,而不是在調試模式下),以前的更改不存在。爲什麼是這樣?無法保存對數據集的更改
//called when the new entry button is clicked, adds new row into DB
private void CreatEnrtyBtn_Click(object sender, EventArgs e)
{
//Create new row to construct
BettingDB1DataSet.HorsesRow row = bettingDB1DataSet.Horses.NewHorsesRow();
//Add values to row
row.HorseName = this.HorseNameBox.Text;
row.Trainer = this.TrainerNameBox.Text;
row.Place = this.PlaceBox.Text;
row.Location = this.LocationBox.Text;
row.Jockey = this.JockeyNameBox.Text;
row.Track = this.LocationBox.Text;
row.Ground = "temp";
//Add row to DB and commit changes
bettingDB1DataSet.Horses.AddHorsesRow(row);
//tried both of these on their own, but neither work?
horsesTableAdapter.Update(bettingDB1DataSet.Horses);
horsesTableAdapter.Update(row);
}
如何創建horsesTableAdapter,如要求:
private BettingDB1DataSetTableAdapters.HorsesTableAdapter horsesAdapter;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bettingDB1DataSet2.Horses' table. You can move, or remove it, as needed.
this.horsesTableAdapter.Fill(this.bettingDB1DataSet2.Horses);
this.dataGridView1.DataSource = bettingDB1DataSet.Horses;
}
您是否遇到任何異常,或者只是正常完成該方法,而不更改數據庫? – 2014-11-20 21:30:25
一切順利。 Row出現在dataGrid中,主鍵是自動添加的,幷包含所有內容。但是當程序再次打開時,沒有更改被保存。 – silent 2014-11-20 21:32:54
@silent您正在描述的問題是,重新啓動程序後,沒有任何內容*被加載到網格中。你是否真的證實了數據庫本身沒有改變? – BartoszKP 2014-11-20 21:35:37