简体   繁体   中英

Simple SQLite query not working

I'm using sqlite compact edition,I done some tests using linqdpad, worked fine. But when I go to C# code, it not works. I tried get fields from database with following code:

using System;
using System.Data.SqlServerCe;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var conStr = @"data source=C:\path\db.sdf;password=...";
            var con = new SqlCeConnection(conStr);
            con.Open();

            var cmd = new SqlCeCommand("select * from quest", con);
            SqlCeDataReader result = cmd.ExecuteReader();
            Console.Write(result.Read());
            Console.ReadLine();
            con.Close();
        }
    }
}

result.Read() returns false . as if had not fields. how I fix it?

Using linqpad I can see something like:

在此处输入图片说明

query:

select * from quest;

UPDATE The problem happens when the database(.sdf) uses a password.

check the semicolon in the end of your query.maybe by delete it your query execute right. in other hand every thing is correct generally.

It looks alright to me - maybe this:

var result 

Change it to SqlCeDataReader. Its the only 'mistake' I can see.

Also don't forget to close the connection after you're done.

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