How can I use the Like Operator with a Parameter in a SQLite query?
The wildcard %
should be added to the parameter value, not to the parameter name
const string qry = "SELECT SiteNum FROM WorkTable WHERE WTName LIKE @wtName";using (SQLiteConnection con = new SQLiteConnection(HHSUtils.GetDBConnection())){ con.Open(); SQLiteCommand cmd = new SQLiteCommand(qry, con); cmd.Parameters.Add(new SQLiteParameter("@wtName", tableName + "%")); siteNum = Convert.ToInt32(cmd.ExecuteScalar());}
And, I am not sure if it matters here, but, usually, I insert the parameter name with the exact name used in the placeholder ( @wtName )
Easiest way to do this is to use '||'
Use :
const string qry = "SELECT SiteNum FROM WorkTable WHERE WTName LIKE @wtName || '%' ";
Instead Of:
const string qry = "SELECT SiteNum FROM WorkTable WHERE WTName LIKE @wtName%";
Try defining query like this:
const string qry = "SELECT SiteNum FROM WorkTable WHERE WTName LIKE @wtName + '%'";