C#Windows 8.1 /获取SQLite字符串查询结果作为变量

时间:2016-12-14 08:43:26

标签: c# windows-store-apps windows-8.1 sqlite-net

如何在变量中获取此SQLite查询结果? 我需要在Windows 8.1应用程序中访问它。

string queryDB = string.Format("Select * from ContryLookup");
var tempcountryLookUpData = connection.Execute(queryDB);

由于

2 个答案:

答案 0 :(得分:0)

您需要参考SqliteDataReader

static void Main() 
{
    string cs = "URI=file:test.db";

    using(SqliteConnection con = new SqliteConnection(cs))
    {
        con.Open();

        string stm = "Select * from ContryLookup";

        using (SqliteCommand cmd = new SqliteCommand(stm, con))
        {
            using (SqliteDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read()) 
                {
                    Console.WriteLine(rdr.GetInt32(0) + " " 
                        + rdr.GetString(1) + " " + rdr.GetInt32(2));
                }         
            }
        }

        con.Close();   
     }
}

Credits

Worth mentioning

答案 1 :(得分:0)

你有一个名为Execute的执行程序吗? 执行所需操作的一种方法是使用ExecuteReader过程并将其返回给DataReader对象。然后使用for循环遍历数据:

string sql = "select * from table";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
       Console.WriteLine(reader["columnName"]);