【发布时间】: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