【问题标题】:C# windows application access database data doesn't persist on closeC# windows 应用程序访问数据库数据在关闭时不会持续存在
【发布时间】:2015-03-09 09:59:52
【问题描述】:

我正在使用 C# 创建一个 Windows 应用程序,我正在访问一个空的 Access 数据库,其中包含两个表:Provinces 和 Locations。我正在处理只处理 Provinces 表的表格,如下所示:

这是一个子表单。当它打开时,我可以插入/更新记录等。每当我进行更改时,我单击“加载表”按钮以显示 DataGridView 对象中的更改。

如果我关闭此子窗体并再次显示它,我可以单击加载表按钮并调用所有数据以在 DataGridView 对象中显示。但是,如果我完全关闭应用程序,那么我会丢失所有数据。我通过双击数据库文件在 Access 中启动它来证明这一点,我可以看到数据肯定已经消失了。这已成为一个谜,因为我无法弄清楚为什么数据不会保留在文件中。请指教。

下面是表单的代码。您可以从我的方法中看到,每次执行功能时,我都会小心地关闭连接对象。所以我不知道为什么我在应用程序关闭时不断丢失数据?

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

namespace GTI_Taxi_Pricing
{
public partial class frmProv : Form
{
    private OleDbConnection connection = new OleDbConnection();
    public frmProv()
    {
        InitializeComponent();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=TaxiDB.accdb;Persist Security Info=False;";                
    }

    private void btnLoad_Click(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            String query = "SELECT * FROM Provinces;";
            command.CommandText = query;
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            connection.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "")
        {
            MessageBox.Show("The name field must have a value.");
            return;
        }
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "INSERT INTO Provinces (name) VALUES ('" + txtName.Text + "')";

            command.ExecuteNonQuery();
            MessageBox.Show("Data Saved");
            connection.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    private void btnNewRecord_Click(object sender, EventArgs e)
    {
        txtID.Text = "";
        txtName.Text = "";
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        if (txtID.Text == "")
        {
            MessageBox.Show("The id field must have a value.");
            return;
        }
        if(txtName.Text == "")
        {
            MessageBox.Show("The name field must have a value.");
            return;
        }
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "UPDATE Provinces SET name='" + txtName.Text + "' WHERE id=" + txtID.Text + ";";

            command.ExecuteNonQuery();
            MessageBox.Show("Data Update Successful.");
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        if (txtID.Text == "")
        {
            MessageBox.Show("The id field must have a value.");
            return;
        }
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "DELETE FROM Provinces WHERE id=" + txtID.Text + ";";

            command.ExecuteNonQuery();
            MessageBox.Show("Record Deleted Successfully.");
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if(e.RowIndex >= 0)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            txtID.Text = row.Cells[0].Value.ToString();
            txtName.Text = row.Cells[1].Value.ToString();
        }
    }
}

}

【问题讨论】:

    标签: c# mysql winforms datagridview ms-access-2010


    【解决方案1】:

    这是基于文件的数据库(或附加的数据库文件)的常见场景
    您的连接字符串不使用任何路径引用数据库。
    这意味着您的数据库位于运行应用程序的同一目录中。
    插入、修改或删除数据没有任何问题,但是当您从 Visual Studio 调试会话中重新启动应用程序时,您会丢失所有内容。

    现在,如果您查看您的项目文件,您可能在其他文件之间列出了数据库文件。在此数据库文件的属性之间,您会注意到属性Copy to the Output directory 及其值设置为Copy Always

    这意味着每次从 Visual Studio 环境中重新启动应用程序时,该文件都会从项目文件夹复制到输出目录(通常是 BIN\DEBUG 或 BIN\x86\DEBUG),但这会破坏在上一次运行删除插入修改或删除的数据

    将属性Copy to Output Directory 更改为Copy NeverCopy if Newer

    但是Copy If Newer 给 MS-Access 带来了另一个问题。如果您使用 Access o 使用 Visual Studio 的服务器连接窗口打开位于项目目录中的数据库文件,如果您不更改任何内容,该文件也会立即修改,因此 Copy If Newer 将执行复制到输出目录

    【讨论】:

    • 哇!你学到的东西……就是这样。现在工作正常。我将属性更改为“请勿复制”。那成功了。非常感谢。
    • 我正在查看您的代码。您还有其他问题,尽管直到现在您还没有注意到它们。连接已关闭但未释放。如果您有异常,则连接保持打开状态。尝试在连接周围使用using statement,最重要的是看看如何编写参数化查询。
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多