// get the LIKE string for a given string, based on where the like wildcards should be private static string GetWildcardedString(string value, LikeLocation loc) { switch (loc) { case LikeLocation.Before: return("%" + value); case LikeLocation.After: return(value + "%"); case LikeLocation.Both: return("%" + value + "%"); default: throw new ArgumentException("invalid LikeLocation provided!"); } }
public int ExistLike(string TableName, string FieldName, string FieldValue, LikeLocation LL) { int res = 0; switch (LL) { case LikeLocation.Start: res = (int)ExecScalar("select Count(*) as NumberOfRows From " + TableName + " Where " + FieldName + "Like %'" + FieldValue + "'"); break; case LikeLocation.End: res = (int)ExecScalar("select Count(*) as NumberOfRows From " + TableName + " Where " + FieldName + "Like '" + FieldValue + "'%"); break; case LikeLocation.Any: res = (int)ExecScalar("select Count(*) as NumberOfRows From " + TableName + " Where " + FieldName + "Like %'" + FieldValue + "'%"); break; } return(res); }
// get the LIKE string for a given string, based on where the like wildcards should be private static string GetWildcardedString(string value, LikeLocation loc) { switch (loc) { case LikeLocation.Before: return "%" + value; case LikeLocation.After: return value + "%"; case LikeLocation.Both: return "%" + value + "%"; default: throw new ArgumentException("invalid LikeLocation provided!"); } }
// perform a LIKE query on a given database for a given column/input string public static DataTable ReadLikeQuery(string table, string column, string value, LikeLocation loc) { value = value.Replace("'", "''"); DataTable dt = new DataTable(); if (_dbConnection.State == ConnectionState.Open) { _dbConnection.Close(); } _dbConnection.Open(); SQLiteCommand com = new SQLiteCommand("select * from " + table + " where " + column + " LIKE '" + GetWildcardedString(value, loc) + "'", _dbConnection); dt.Load(com.ExecuteReader()); _dbConnection.Close(); return dt; }
// perform a LIKE query on a given database for a given column/input string public static DataTable ReadLikeQuery(string table, string column, string value, LikeLocation loc) { value = value.Replace("'", "''"); DataTable dt = new DataTable(); if (_dbConnection.State == ConnectionState.Open) { _dbConnection.Close(); } _dbConnection.Open(); SQLiteCommand com = new SQLiteCommand("select * from " + table + " where " + column + " LIKE '" + GetWildcardedString(value, loc) + "'", _dbConnection); dt.Load(com.ExecuteReader()); _dbConnection.Close(); return(dt); }