有没有办法使用 foreach 遍历 SQLite 数据库并按行获取和使用数据?

问题描述 投票:0回答:2

我正在 winforms 中创建一个项目,它应该模仿一个库。用户登录,创建一本“书”(实际上只是控件或用户控件的集合,其中包含存储信息的变量),将其信息存储在 SQLite 数据库中,然后程序将其显示在 flowLayoutPanel 中。我的问题是,现在,我需要找到一种方法,在加载表单时在 flowLayoutPanel 内显示该用户的ALL“书籍”,因为“书籍”将在重新启动程序时被删除。

database table for books。 在数据库中我的图书表中,有一个字段用于创建该图书的用户的用户名。我想过使用 foreach 使用用户名作为关键字浏览表格,获取数据行,并使用每个信息创建一本“书”,如下所示:

using (IDbConnection cnn = new SQLiteConnection(SqliteDataAccess.loadConnectionString()))
{
    foreach(string username in booksTable)
    {
        bookUserControl createNewBook = new bookUserControl();

        createNewBook.guid = cnn.QuerySingle<String>("SELECT guid FROM booksTable WHERE username = " + username);
        createNewBook.username = username;
        createNewBook.title = cnn.QuerySingle<String>("SELECT title FROM booksTable WHERE username = " + username);
        createNewBook.author = cnn.QuerySingle<String>("SELECT author FROM booksTable WHERE username = " + username);
        createNewBook.date = cnn.QuerySingle<String>("SELECT date FROM booksTable WHERE username = " + username);
        createNewBook.desc = cnn.QuerySingle<String>("SELECT desc FROM booksTable WHERE username = " + username);
        createNewBook.photo = byteToImage(cnn.QuerySingle<byte[]>("SELECT image FROM booksTable WHERE username = " + username);); 
        //image is stored as an array of bytes so I need to convert it from byte[] to Image

        //creates the "book" inside the flowLayoutPanel
        flowLayoutPanel1.Controls.Add(createNewBook);
    }
}

问题是我不知道如何使用表作为参数,而且我在网上看到的大多数都使用列表或其他不适合的东西。关于更好的方法有什么建议吗? (如果这真的有效的话)

c# winforms sqlite
2个回答
1
投票

如果您使用 Dapper,您可以直接反序列化到

bookUserControl

using IDbConnection cnn = new SQLiteConnection(SqliteDataAccess.loadConnectionString());
cnn.Open();

foreach(string username in booksTable)
{
    const string query = @"
SELECT
  guid,
  username,
  title,
  author,
  date,
  desc,
  image
FROM booksTable
WHERE username = @username;
";
    bookUserControl createNewBook = cnn.QuerySingle<bookUserControl>(query, new {username});
    createNewBook.photo = byteToImage(createNewBook.image); 
    //image is stored as an array of bytes so I need to convert it from byte[] to Image

    //creates the "book" inside the flowLayoutPanel
    flowLayoutPanel1.Controls.Add(createNewBook);
}

注意使用适当的参数化,而不是注入数据。


0
投票

我们经常使用

while
循环,就像这样

using (IDbConnection cnn = new SQLiteConnection(SqliteDataAccess.loadConnectionString())) {
  cnn.Open();

  string sql = 
    @"SELECT guid, 
             title,
             author,
             date
             /* Add More Fileds if you want */
        FROM booksTable
       WHERE username = @username";

  using (SQLiteCommand q = new SQLiteCommand(sql, cnn)) {
    q.Parameters.Add("@username", SqliteType.Text).Value = userName;

    using (var reader = q.ExecuteReader()) {
      while (reader.Read()) {
        bookUserControl createNewBook = new bookUserControl();

        createNewBook.username = username;

        createNewBook.guid = Convert.ToString(reader[0]);
        createNewBook.title = Convert.ToString(reader[1]);
        ...
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.