简体   繁体   English

C# 声称我的 SQLite 表不存在,但确实存在

[英]C# claims my SQLite table doesn't exist but it does

I'm trying to read a table I created with DB Browser for SQLite, but there's a runtime error claiming the database doesn't have the table.我正在尝试读取使用 DB 浏览器为 SQLite 创建的表,但出现运行时错误,声称数据库没有该表。 But it does.但确实如此。 I created it and I can see it in DB Browser.我创建了它,我可以在 DB Browser 中看到它。

Here's the code:这是代码:

    private void PopulateGridCustomers()
    {
        String conString = Properties.Resources.database;
        var con = new SqliteConnection(conString);
        try
        {
            con.Open();
            Console.WriteLine(con.State);
            SqliteCommand cmd = con.CreateCommand();
            cmd.CommandText = "SELECT * FROM Clientes";
            using (SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(cmd.CommandText, conString))
            {
                DataTable dt = new DataTable();
                dataAdapter.Fill(dt);
                gridCustomers.DataSource = dt;
            }
        }

And a picture showing the table exists indeed:一张显示该表的图片确实存在:

在此处输入图像描述

What could be causing this issue and how to fix it?什么可能导致此问题以及如何解决?

Thanks谢谢

PS: Please cut me some slack, I'm not a professional developer. PS:请放过我,我不是专业的开发人员。 I code for myself.我为自己编码。

Update: Here's the actual error message:更新:这是实际的错误消息:

System.Data.SqlServerCe.SqlCeException: 'The specified table does not exist. 
[ Clientes ]'

UPDATE 2:更新 2:

Here's the path of the database:这是数据库的路径:

Data Source=C:\Users\charl\OneDrive\Documentos\SBM\Database\SBMTeste.db;

As you can see, it is the very same that shows in DB Browser window.如您所见,它与 DB Browser window 中显示的完全相同。 The error I first described occurs if I omit the ".db".如果我省略“.db”,就会出现我第一次描述的错误。 If I put the file extension in the string, the following error is thrown:如果我将文件扩展名放在字符串中,则会引发以下错误:

System.Data.SqlServerCe.SqlCeException: 'The database file may be corrupted. 
Run the repair utility to check the database file. 
[ Database name = C:\Users\charl\OneDrive\Documentos\SBM\Database\SBMTeste.db ]'

UPDATE 3:更新 3:

Here's the CREATE statement of the table:这是表的 CREATE 语句:

CREATE TABLE "Clientes" (
    "id"    INTEGER NOT NULL UNIQUE,
    "nome"  TEXT NOT NULL,
    "telefone1" TEXT,
    "telefone2" TEXT,
    "email" TEXT,
    "endereço"  TEXT,
    "bairro"    TEXT,
    "cidade"    TEXT,
    "data_nascimento"   TEXT,
    "data_cadastro" TEXT DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY("id" AUTOINCREMENT),
    UNIQUE("nome","data_nascimento")
)

SqlCe is not Sqlite, SqlCe 不是 Sqlite,

below an extract of my code using sqlite:下面是我使用 sqlite 的代码摘录:

public Equity SearchEquityByEpic(string epic)
{
    string query = string.Format("SELECT * from " + Tables.ASSETS_TABLE + " WHERE epic='{0}'", epic);
    Log(query);
    var command = _connection.CreateCommand(query);
    var equities = command.ExecuteQuery<Equity>();
    if (equities.Count == 0)
    {
        return null;
    }
    Equity ret = equities.First();
    return ret;
}

The thing is I messed up when trying to install the packages needed for my application to work with the SQlite database.问题是我在尝试安装我的应用程序所需的软件包以使用 SQlite 数据库时搞砸了。 I endend up installing a lot of unnecessary things and missing using System.Data.SQLite .我最终使用System.Data.SQLite安装了很多不必要的东西并丢失了。 That way my application was not being able to recognize the.db file hence the "corrupted file" message described in my 3rd update.这样我的应用程序就无法识别 .db 文件,因此在我的第三次更新中描述了“文件损坏”消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM