简体   繁体   中英

query on system.data.sqlite not working

I have a sqlite database with table called File, that has a column called FilePath, of type Text.

On that table there is an entry whose FilePath has the value f9a35e24-bce9-46c8-bbc0-02a005455fe3 (the toString of a random GUID).

If I try the following query on SQLiteStudio, it outputs the entry.

SELECT FilePath FROM File WHERE FilePath = 'f9a35e24-bce9-46c8-bbc0-02a005455fe3'

However, using the code below, that uses the System.Data.SQLite external assembly, it never retrieves the entry.

SQLiteConnection conn = new SQLiteConnection(connectionString);

SQLiteCommand cmd = 
    new SQLiteCommand(@"SELECT FilePath FROM File WHERE FilePath = '@Where';", conn);

cmd.Parameters.Add("@Where", DbType.String).Value = 
    "f9a35e24-bce9-46c8-bbc0-02a005455fe3";

conn.Open();

SQLiteDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
    Console.WriteLine("yes");
}
else
{
    Console.WriteLine("not");
}

Am I missing something?

You don't need single quote ' around the @where parameter. Your code should be like this:

SQLiteCommand cmd = 
new SQLiteCommand(@"SELECT FilePath FROM File WHERE FilePath = @Where", conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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