VS2017 SQLite没有将数据写入数据库

时间:2017-12-03 04:23:41

标签: sqlite dataset visual-studio-2017 dataadapter

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

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

        }
    }
}

1 个答案:

答案 0 :(得分:1)

请查看代码的这一部分

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