0

我正在通过 Windows 窗体将记录添加到我的数据库中。但是,当我添加新记录时,它不会更新,直到我关闭应用程序然后重新开始。即使我认为我是在告诉它更新(我不是!!)

我需要一个新的变量来更新数据库吗?我有点卡住了。

编辑:关于这个的所有代码..

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();
    }
}

}

4

2 回答 2

0

从主表单打开 AddMedia 表单后,您需要再次 RALOAD 所有记录。查看表单在打开时的执行方式,并在您打开 AddMedia 表单后执行相同的操作;)

于 2012-05-10T23:50:54.577 回答
0

最好使用 DataSet 然后更新其中的记录。最后,您需要在 update() 命令之后调用 AcceptChanges() 以使用数据集提交更改,如下所示,还需要在输入后重新加载数据以获取最近的数据信息: dataset.AcceptChanges()

于 2012-05-10T23:57:02.297 回答