【问题标题】:VS2017 SQLite not writing data to databaseVS2017 SQLite 未将数据写入数据库
【发布时间】:2018-05-16 20:21:16
【问题描述】:

我有以下代码,它不会抛出异常,但它也不会更新 SQLite 数据库,有两种方法,一种由更新按钮调用,从 GridView 控件获取更新的数据并写入 SQLite 文件.两者都没有真正更新数据库。在更新方法中,有几种尝试使用不同的技术来尝试写入数据。我将整个内容包含在上下文中,因为功能被分成不同的代码部分。它使用 Metro 框架,但我认为这对数据库代码没有影响。

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

        }
    }
}

【问题讨论】:

    标签: sqlite dataset visual-studio-2017 dataadapter


    【解决方案1】:

    请查看您的这部分代码

            DataTable dt = new DataTable();
            empDataAdapter.Fill(dt);
            empDataAdapter.Update(dt);
    

    您正在获取数据并将其直接写回数据库。如果您不更改任何内容,则不会在 db 中看到任何更改。我看不出这段代码的意义

    代码跟在后面

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

    【讨论】:

    • 谢谢您,这解决了问题。正如您所说,Update() 需要在记录更改后运行,这是有道理的。也感谢你浏览了这么大的一段代码。
    猜你喜欢
    • 2019-01-03
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多