首页 > 解决方案 > 将 SQLite 选择查询转换为字符串数组

问题描述

我有这个代码

public static void GetOnline1()
        {
            string query = "SELECT online FROM online";
            SQLiteCommand myCommand = new SQLiteCommand(query, myConnection);
            myConnection.Open();
            SQLiteDataReader result = myCommand.ExecuteReader();
            if (result.HasRows)
            {
                while (result.Read())
                {
                    Console.WriteLine(result["online"]);
                    //result["online"] to string array?
                }
            }
            myConnection.Close();

我如何将结果 [“在线”] 转换为字符串数组?

标签: c#sqlsqlite

解决方案


您需要在之前创建一个新的字符串列表if(result.HasRows)

var list = new List<string>();

然后在 while 循环中添加result["online"]到列表中,如下所示。

while(result.Read())
{
     list.Add(result["online"].ToString());
}

推荐阅读