2012-05-10 48 views
0

我正在通過Windows窗體向我的數據庫添加記錄。但是,當我添加新記錄時,它不會更新,直到我關閉該應用程序,然後重新開始。即使我想我告訴它更新(Im obv不!!)以窗體形式更新數據庫和c#

我需要一個新的varibale來更新數據庫嗎?我有點卡住了。

編輯:在這所有的代碼..

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace MediaManagementSystem 
{ 
public partial class AddMedia : Form 
{ 
    //database var 
    OleDbConnection m_cnADONetConnection = new OleDbConnection(); 
    OleDbDataAdapter m_daDataAdapter; 
    OleDbDataAdapter m_cbCommandBuilder; 
    DataTable m_dtMedia = new DataTable(); 
    int m_rowPosition = 0; 

    public AddMedia() 
    { 
     InitializeComponent(); 
    } 

    private void BrowseButton_Click(object sender, EventArgs e) 
    { 
     //load up file dialog and find media 
     if (addFileDialog.ShowDialog() == DialogResult.OK) 
     { 
      //add media file name to file path text box 
      txtFilePath.Text = addFileDialog.FileName; 
     } 
    } 

    private void CancelButton_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void AddButton_Click(object sender, EventArgs e) 
    { 
     //add the new record to the database 
     DataRow drNewRow = m_dtMedia.NewRow(); 
     drNewRow["FilePath"] = txtFilePath.Text; 
     drNewRow["Subject"] = txtSubject.Text; 
     drNewRow["Title"] = txtTitle.Text; 
     drNewRow["Keywords"] = txtKeywords.Text; 
     drNewRow["MediaType"] = AddComboBox.Text; 
     m_dtMedia.Rows.Add(drNewRow); 

     m_daDataAdapter.Update(m_dtMedia); 
     m_rowPosition = m_dtMedia.Rows.Count - 1; 
     this.ShowCurrentRecord(); 
     this.Close(); 
    } 

    private void AddMedia_Load(object sender, EventArgs e) 
    { 
     //link to the database and conect to database 
     m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Max\Documents\Visual Studio 2010\Projects\MediaManagementSystem\MediaManagementSystem\bin\Debug\MediaDB.mdb"; 
     m_cnADONetConnection.Open(); 
     OleDbConnection objConnection = new OleDbConnection(m_cnADONetConnection.ConnectionString); 

     m_daDataAdapter = new OleDbDataAdapter("Select * From Media", m_cnADONetConnection); 

     OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter); 
     m_daDataAdapter.Fill(m_dtMedia); 
     m_daDataAdapter.Update(m_dtMedia); 
    } 

    public void ShowCurrentRecord() 
    { 
     m_daDataAdapter.Update(m_dtMedia); 

     if (m_dtMedia.Rows.Count == 0) 
     { 
      txtFilePath.Text = ""; 
      txtSubject.Text = ""; 
      txtTitle.Text = ""; 
      txtKeywords.Text = ""; 
      AddComboBox.Text = ""; 
      return; 
     } 
     txtFilePath.Text = m_dtMedia.Rows[m_rowPosition]["FilePath"].ToString(); 
     txtSubject.Text = m_dtMedia.Rows[m_rowPosition]["Subject"].ToString(); 
     txtTitle.Text = m_dtMedia.Rows[m_rowPosition]["Title"].ToString(); 
     txtKeywords.Text = m_dtMedia.Rows[m_rowPosition]["Keywords"].ToString(); 
     AddComboBox.Text = m_dtMedia.Rows[m_rowPosition]["MediaType"].ToString(); 
    } 
} 

}

+0

「它不更新直到我關閉應用程序,然後重新開始「所以你關閉它並重新打開它,你會看到你的改變? – banging

+0

旁註:你打電話給'm_daDataAdapter.Update(m_dtMedia);'當你AddButton_Click時,調用兩次# – banging

+0

是的,這是正確的 –

回答

0

如果您使用DataSet並更新其中的記錄,那最好。最後你需要在update()命令之後調用AcceptChanges()來提交數據集的更改,如下所示,在進入以獲取最新數據信息之後重新加載數據: dataset.AcceptChanges()

+0

對不起,我不關注 –

0

從主窗體您打開AddMedia表格後,您需要重新RALOAD所有記錄。看看它打開時的表單是如何操作的,並在打開AddMedia表單後執行相同的操作;)

+0

對不起,這是因爲我有其他事情要做,所以我會採取。如果你真的想嘗試,你應該能夠從這裏弄清楚它! – banging

+0

'code private void AddButton_Click(object sender,EventArgs e) { AddMedia AddMedia = new AddMedia(); AddMedia.Show(); m_daDataAdapter.Update(m_dtMedia); 「還沒有。不應該當我關閉添加表單然後重新加載? –

+0

在你的MAIN上,你用什麼來顯示你所有的記錄? DataGridView的?不管它是什麼,它都沒有更新! – banging