DataGridView无法反映数据库上的更改

时间:2019-02-12 02:56:23

标签: c# datagridview sqlite

我的C#Datagridview不显示更新的数据。

这是一个使用SQLite的Winform c#应用程序。当我的表单打开时,它以我使用DB Browser for SQLite更新两行中的一列之前的样子显示数据。在执行以下SQL时,在数据库浏览器中:SELECT * FROM SPEAKERS,我在更改的列中看到10和13(它们以前是0和2。此列定义为INTEGER。

当我打开特定表格时,该列中的数据显示为0和2。在网格的更下方,我看到12和13,依此类推,因此该列不是格式问题。在此表单上,我有一个hCDataset,一个扬声器绑定源和一个扬声器表适配器。当我右键单击扬声器表适配器并选择“预览数据”时,它将显示HCDataSet以及层次结构下的Speakers表以及所选对象:HCDataSet.Speakers.Fill.Getdata()方法。当我点击“预览”按钮时,数据看起来就像在我的数据网格上一样。

相信我,我已经重新启动,关闭了所有内容,回到了SQLite的数据库浏览器,并且每次在这些列中打开10和13时,但从未在这些表格的网格中显示该列中的新更新值。

我以为我可以截图,但显然不能。我可以将屏幕快照粘贴到Paint中,它们看起来不错,但是显然当我从那里复制并在此编辑器中按CTRL-G时,它们不会显示在本文档中。也许我没有足够高的权限或什么。

我在其他帖子上读到,人们在从网格中更新问题,而在数据库中看不到更新。我在更新数据库时遇到了相反的问题,但是由于某种原因,网格未显示它。

我已经在向您展示我的连接方法的代码和用于查询数据库的代码中添加了代码。当我到达第三行时,它在RANK列中显示正确的“ 10”!

public static SQLiteCommand GetSqliteCommand()
{
    SQLiteConnection conn = GetSqliteConnection();
    return conn.CreateCommand();
}

public static SQLiteConnection GetSqliteConnection() 
{
    string dbPath = Path.Combine(Environment.CurrentDirectory, "hc.db");
    string connectionString = string.Format("Data Source={0};foreign keys=true", dbPath);
    SQLiteConnection conn = new SQLiteConnection(connectionString);
    conn.Open();
    return conn;
}


//Now my code snippet to read the database and this DOES return the updated column!

GetLastUpdateSql = "SELECT * FROM SPEAKERS ORDER BY ID";

cmd.CommandText = GetLastUpdateSql;
cmd.ExecuteNonQuery();

SQLiteDataReader reader;
reader = cmd.ExecuteReader();

int int1, int2, int3, int4, int5;
string fn, ln;

while (reader.Read())
{
    fn = reader["FIRST_NAME"].ToString();
    ln = reader["LAST_NAME"].ToString();
    int1 = Convert.ToInt32(reader["ID"]);
    int2 = Convert.ToInt32(reader["TYPE"]);
    int3 = Convert.ToInt32(reader["WARD"]);
    int4 = Convert.ToInt32(reader["RANK"]);
    int5 = Convert.ToInt32(reader["ACTIVE"]);
}

0 个答案:

没有答案
相关问题