1

I have a database with 1 table that has 1 column that keeps of my file watcher. I want the user to be able to search for a file extension of their choice but am having a hard time getting it to work correctly. I can query for all file extensions, but when I try to use user input as a variable something seems to not be working correctly. (Also using wildcard character for searching). My issue is primarily only with the select statement.(Or so I think.)

string str = textBox1.Text.Trim();

query = "SELECT * FROM LOG WHERE ENTRY like '%@str%'";

using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
{
    using (SQLiteDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            listBox1.Items.Add((string) reader["ENTRY"]);
        }
    }
}

1 Answer 1

1

You are not setting the parameter to the query. Also, you need to separate the wildcards from the parameter in the query text. Try this:

string str = textBox1.Text.Trim();

query = "SELECT * FROM LOG WHERE ENTRY like '%' || @str || '%'";

using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
{
    // Might need a different data type here
    cmd.Parameters.Add("@str",  SQLiteType.Text).Value = str;

    using (SQLiteDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            listBox1.Items.Add((string)reader["ENTRY"]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.