如何在sqlite中使用c#列出带有通配符的表项

本文关键字:通配符 sqlite | 更新日期: 2023-09-27 18:15:14

如何使用通配符从表中获取元素?

我写过类似的代码。这里出了什么问题?如果我把它放入循环中安全吗?

private void Pokaz()
    {
        String sql = "SELECT [element] FROM [table] LIKE @Word";
        SQLiteConnection connection = new SQLiteConnection(@"Data Source=C:'Temp2'dictionary.s3db");
        connection.Open();
        SQLiteCommand cmd = new SQLiteCommand(connection);
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("@Word", "%" + "dog" + "%");
        DataTable dt = new DataTable();
        SQLiteDataReader reader = cmd.ExecuteReader();
        dt.Load(reader);
        reader.Close();
        connection.Close();
        dataGridView1.DataSource = dt;
    }

我更改为String sql = "SELECT [element] FROM [table] where [element] LIKE ''@Word''"; ,但现在我得到空结果。


我也试过这个方法。固定和工作。

List<string> list = new List<string>();
        string connectionString = @"Data Source=C:'Temp2'dictionary.s3db";
        string sql = "SELECT [element] FROM [table] where [element] LIKE  ''%dog%'' ";
        using (var connection = new SQLiteConnection(connectionString))
        {
            using (var command = new SQLiteCommand(sql, connection))
            {
                connection.Open();
                SQLiteDataReader rd = command.ExecuteReader();
                while (rd.Read())
                {
                    list.Add(rd[0].ToString());
                }       
            }
        }

如何在sqlite中使用c#列出带有通配符的表项

你试过了吗

String insSQL = "SELECT [element] FROM [table] like @Word"; //% search with prefix and postfix
SQLiteCommand cmd = new SQLiteCommand(insSQL);
cmd.Parameters.AddWithValue("@Word", "%" + "dog" + "%");

?