VS2017 SQLite不写数据到数据库
我有下面的代码,它不会抛出异常,但它也不会更新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();
}
}
}
回答:
请看看你的代码
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"]);
的接受改变,标志着所有记录作为没有改变。因此,您的DataAdapter.Update不会看到任何更改的记录发送到数据库。我会将该行代码移动到DataAdapter.Updates之后,然后再次尝试更新记录。
empDataAdapter.Update(data, "EMP"); departmentDataAdapter.Update(data, "DEPARTMENT");
empDataAdapter.Update(data.Tables["EMP"]);
departmentDataAdapter.Update(data.Tables["DEPARTMENT"]);
data.AcceptChanges();
以上是 VS2017 SQLite不写数据到数据库 的全部内容, 来源链接: utcz.com/qa/259475.html