【问题标题】:Show Database in listbox在列表框中显示数据库
【发布时间】:2020-08-01 20:03:52
【问题描述】:

我想在数据库(sqlite)中显示用户名列表,这是我的代码

当我运行程序时什么都没有显示

private void UserList_SelectedIndexChanged(object sender, EventArgs e)
    {
        connectionString conn = new connectionString();
        string query = "SELECT  Username FROM Userdata";
        SQLiteCommand test = new SQLiteCommand(query, conn.myconnection);
       var non = test.ExecuteReader();
        foreach (var USR in non)
        {
            UserList.Items.Add(USR);

        }

    }

【问题讨论】:

    标签: c# database visual-studio winforms sqlite


    【解决方案1】:

    您需要使用SQLiteDataReader 来读取针对 SQLite 数据库执行的命令的结果。

    你可以参考下面的demo。

    SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
    m_dbConnection.Open();
    
    SQLiteCommand sqlCom = new SQLiteCommand("SELECT Username FROM Userdata", m_dbConnection);
    SQLiteDataReader sqlDataReader = sqlCom.ExecuteReader();
    
    while (sqlDataReader.Read())
    {
        listBox1.Items.Add(sqlDataReader.GetValue(0));
    }
    
    m_dbConnection.Close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多