0
我有下面的代碼,它不會拋出異常,但它也不會更新SQLite數據庫,有兩種方法,一種是由Update按鈕調用的,並從GridView獲取更新的數據控制並寫入SQLite文件。實際上都沒有更新數據庫。在更新方法中,有幾次嘗試使用不同的技術來嘗試寫入數據。我將整個事件包含在上下文中,並且功能被分解爲代碼的不同部分。它使用的是Metro Framework,但我認爲這對數據庫代碼沒有什麼影響。VS2017 SQLite不寫數據到數據庫
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using MetroFramework;
using System.Data.SQLite;
namespace MetroTestApp
{
public partial class Form1 : MetroFramework.Forms.MetroForm
{
static List<String> entries = new List<string>();
private BindingSource masterBindingSource = new BindingSource();
private BindingSource detailsBindingSource = new BindingSource();
DataSet data = new DataSet();
static string DbConnectionString = @"Data Source=Emp.db;Version=3;";
static SQLiteConnection dbc = new SQLiteConnection(DbConnectionString);
public SQLiteDataAdapter departmentDataAdapter;
SQLiteDataAdapter empDataAdapter;
DataRelation relation;
SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder();
public Form1()
{
InitializeComponent();
}
private void metroTile1_Click(object sender, EventArgs e)
{
MetroFramework.MetroMessageBox.Show(this, "OK", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void metroTile2_Click(object sender, EventArgs e)
{
MetroFramework.MetroMessageBox.Show(this, "Stop", "Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop);
}
private void metroGrid2_CellContentClick(object sender, EventArgs e) { }
private void metroButton2_Click(object sender, EventArgs e) { }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
dbc.Close();
cmdBuilder.Dispose();
departmentDataAdapter.Dispose();
empDataAdapter.Dispose();
masterBindingSource.Dispose();
empDataAdapter.Dispose();
data.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
// string DbConnectionString = @"Data Source=Emp.db;Version=3;";
// using (SQLiteConnection dbc = new SQLiteConnection(DbConnectionString))
try
{
dbc.Open();
departmentDataAdapter = new SQLiteDataAdapter("select * from DEPARTMENT", dbc);
empDataAdapter = new SQLiteDataAdapter("select * from EMP", dbc);
departmentDataAdapter.Fill(data, "DEPARTMENT");
empDataAdapter.Fill(data, "EMP");
DataRelation relation = new DataRelation("EMPDPEP", data.Tables["DEPARTMENT"].Columns["DEPNO"], data.Tables["EMP"].Columns["DEPNO"]);
data.Relations.Add(relation);
masterBindingSource.DataSource = data;
masterBindingSource.DataMember = "DEPARTMENT";
detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "EMPDPEP";
DEPGridView.DataSource = masterBindingSource;
EMPGridView.DataSource = detailsBindingSource;
DEPGridView.AutoResizeColumns();
EMPGridView.AutoResizeColumns();
}
catch (SQLiteException ex)
{
MetroFramework.MetroMessageBox.Show(this, "Stop", ex.Message.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
private void metroButton3_Click(object sender, EventArgs e)
{
//dbc.Open();
//departmentDataAdapter = new SQLiteDataAdapter("select * from DEPARTMENT", dbc);
//empDataAdapter = new SQLiteDataAdapter("select * from EMP", dbc);
SQLiteCommandBuilder cb=new SQLiteCommandBuilder(empDataAdapter);
empDataAdapter.DeleteCommand = cb.GetDeleteCommand(true);
empDataAdapter.UpdateCommand = cb.GetUpdateCommand(true);
empDataAdapter.InsertCommand = cb.GetInsertCommand(true);
SQLiteCommandBuilder cb1 = new SQLiteCommandBuilder(departmentDataAdapter);
departmentDataAdapter.DeleteCommand = cb1.GetDeleteCommand(true);
departmentDataAdapter.UpdateCommand = cb1.GetUpdateCommand(true);
departmentDataAdapter.InsertCommand = cb1.GetInsertCommand(true);
// MetroFramework.MetroMessageBox.Show(this, cb.GetUpdateCommand().ToString(), "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
empDataAdapter.AcceptChangesDuringUpdate = true;
departmentDataAdapter.AcceptChangesDuringUpdate = true;
DataTable dt = new DataTable();
empDataAdapter.Fill(dt);
empDataAdapter.Update(dt);
data.AcceptChanges();
empDataAdapter.Update(data, "EMP");
departmentDataAdapter.Update(data, "DEPARTMENT");
empDataAdapter.Update(data.Tables["EMP"]);
departmentDataAdapter.Update(data.Tables["DEPARTMENT"]);
dt.Dispose();
cb.Dispose();
cb1.Dispose();
dbc.Close();
}
private void metroButton4_Click(object sender, EventArgs e)
{
dbc.Open();
DataTable t; t = data.Tables["DEPARTMENT"];
DataRow newRow;
newRow = t.NewRow();
newRow["DEPNO"] = 10; newRow["DEPNAME"] = "GAMES";
t.Rows.Add(newRow);
data.AcceptChanges();
DEPGridView.Refresh();
dbc.Close();
t.Dispose();
}
}
}
謝謝,解決了這個問題。正如你所說,有意義的是,Update()需要在記錄更改後運行。並感謝您閱讀這麼大的一段代碼。 –