When I run this command in MySQL:
SELECT * FROM v
WHERE v.firstname LIKE '%a%' OR middlename LIKE '%a%' OR lastname LIKE '%a%'
It returns 4 rows in the result set.
But when I run the same query using parameters in C# it returns only one.
SELECT * FROM v
WHERE v.firstname LIKE ?word OR middlename LIKE ?word OR lastname
cmd.Parameters.AddWithValue("word", '%'+key+'%');
I also tried '%' ?word '%' and adding the parameter (key) only, but this didn't work either.
How do I make this work?
Are you using something like:
cmd.ExecuteScalar()
in your code? It will return only 1 record. Try
cmd.ExecuteReader()
instead.
It looks like you're missing "LIKE ?word" at then end of your second SQL statement.
WHERE v.firstname LIKE ?word OR middlename LIKE ?word OR lastname
are you missing lastname LIKE ?word ?
Related
I am having trouble using parameters for my in line oraclae query in c#. Why does the parameter not work within the wildcard?
This line returns no results:
Select id, name from Users where UPPER(name) like '%:name%'
command.Parameters.Add("name", OracleDbType.Varchar2, name.ToUpper(), ParameterDirection.Input);
But this returns:
Select id, name from Users where UPPER(name) like '%" + name.ToUpper() +"%'
Did you mean this?
Select id, name
from Users
where UPPER(name) like '%' ||:name.ToUpper()||'%'
This concatenates your C# variable and the Oracle wildcard characters.
I have this code:
string query = "SELECT * FROM dbo.customer WHERE mobileNumber Like '%#mobileNumber'";
When I execute that code from c#, I got empty results
However when I execute this query in my sql managmenet studio
SELECT * FROM dbo.customer WHERE mobileNumber Like '%454545'
I got result.
What I am missing?
Try this:
string query = "SELECT * FROM dbo.customer WHERE mobileNumber Like '%' + #mobileNumber
This is assuming #mobileNumber is a parameter containing the value for which you want to search.
It seems from your code that #mobileNumber is a parameter. Yet, you place it here as a string.
Try putting the % in a string and the parameter separately unquoted:
string query = "SELECT * FROM dbo.customer WHERE mobileNumber Like '%' + #mobileNumber";
I have a table which has three columns: firstname, middlename, lastname. Now, when I type the first letter of each character, the query have to return correctly results.
Example: My database has one record:
When I type the input string as below (just only bold string), the returned results must have the record:
**n** or
**n k** or
**n p** or
**k p** or
**n k p**
I use SQLite, and at the currrent I do not have any idea.
Could you give me a suggestion about this?
You could use wildcard % character to match any string/character and the LIKE statement as well.
For first name that starts with 'N' for example would be:
SELECT * FROM yourTable WHERE FirstName LIKE 'N%'
For first and last name it would be:
SELECT * FROM yourTable WHERE FirstName LIKE 'N%' or LastName LIKE 'P%'
Update:
And if you are not sure if the input is for first name, middle name or last name then you can do it on multiple fields, like:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'N%' or LastName LIKE 'N%'
And if there are more than one letter (say two) it would get a little crazier like:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'N%' or LastName LIKE 'N%'
or FirstName LIKE 'P%' or MiddleName LIKE 'P%' or LastName LIKE 'P%'
But using the solution above would not be good. So, it would be good that you allow the users to either:
Input letters for each name, that is, a letter for lastname and firstname and middlename if needed
Just input one letter (or of course a String) then search either from firstname, lastname and middlename.
For option number 1 you query would look like this:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'K%' or LastName LIKE 'P%'
For option number 2 you query would look like this:
SELECT * FROM yourTable
WHERE FirstName LIKE 'N%' or MiddleName LIKE 'N%' or LastName LIKE 'N%'
or if it a String it would probably look like this:
SELECT * FROM yourTable
WHERE FirstName LIKE 'Ngu%' or MiddleName LIKE 'Ngu%' or LastName LIKE 'Ngu%'
In other words take control of how the users input and give him/her little room for potential error not to mention confusion.
Hi I have a select and a text type input. These controls are used to filter list in a datagrid. The select control has the following options: LastName, MiddleName, FirstName, Gender, City and StudentID. Then I was given this query:
SELECT * FROM Student
WHERE ( LastName LIKE '%' + #LastName + '%' OR #LastName = '' )
AND ( MiddleName LIKE '%' + #MiddleName + '%' OR #MiddleName = '' )
AND ( FirstName LIKE '%' + #FirstName + '%' OR #FirstName = '' )
AND ( Gender LIKE '%' + #Gender + '%' OR #Gender = '' )
AND ( City LIKE '%' + #City + '%' OR #City = '' )
AND ( StudentID LIKE '%' + #StudentID + '%' OR #StudentID = '' )
So for example, the user selects First Name then typed in "James". That could be read as "Give me the list of students whose First Name is 'James'." In my controller, the only way I thought is using If..Else If or Switch condition. It was like this:
string lastName = String.Empty;
string middleName = String.Empty;
and so on..
if(dropdown == "Lastname")
{
lastName = textbox_value;
}
else if(dropdown == "Middlename")
{
middleName = textbox_value;
}
and so on..
Then I have method which will pass the values as parameters to my sql query.
resultList = GetRecord(lastName, middleName, firstName, gender, city, studentID);
My question now is, what is the better way, the cleanest and simplest, than using If..Else and Switch?
You need to decide it somewhere , either in your controller or you can delegate this to another class but delegating it to another helper class will takeout the responsibility. so i would say , you may think of below design
public class ParameterSelector
{
private Dictionary<string,Action> _dictActionMap = new Dictionary<string, Action>();
public ParameterSelector()
{
PopulateDictionaryWithActions();
}
private void PopulateDictionaryWithActions()
{
_dictActionMap.Add("LastName", () => lastName = txtBox_Value);
_dictActionMap.Add("MiddleName", () => middleName = txtBox_Value);
}
}
and usage
main()
{
if(_dictActionMap.ContainsKey(dropdown))
_dictActionMap[_dictActionMap]();
}
see to avoid ifs and switches , i used dictionary with relevant actions, this approach has its own drawback for example , we have unnessary actions defined to handle the part of switch / ifs , its give a feeling that we have get rid of ifs/switch but have tradeoffs with memory requirement so never complicate things which can be handle with simplicity.
so i would say using of Ifs/ Switch has nothing wrong in your case.
You can look at using a switch (C# Reference) statement.
The switch statement is a control statement that selects a switch
section to execute from a list of candidates.
I'm having a problem with subsonic query. The deal is, I have a view and I want to query it's data to produce something like the following SQL statement:
select *
from myView
where (col1 like '%a%' or col2 like '%a%' or col3 like '%a%')
and col4 = 1 and col5 = 2
But instead the query that is submited to the DB is something like this:
select *
from myView
where col1 like '%a%' or col2 like '%a%' or col3 like '%a%'
and col4 = 1 and col5 = 2
Is there a way to do something like the fisrt query?
Please note I'm using .net 2.0 and subsonic 2.2
Thank you in advance.
Even do, Subsonic rules!
You need to use the Constraint Expressions: WhereExpression, AndExpression, OrExpression, EndExpression.
Anything following “WhereExpression” (or Or/AndExpression) will be wrapped in parentheses. You can close the expression by using “CloseExpression()”, or it will be closed for you if another is started (as with OrExpression above) or if the query ends.
Also see: Using Nested Where/And/Or.
If you post your current code maybe I will be able to give more specific answer but basically you have to start WhereExpression.
For example code like this:
var usersQuery = new Select(new SubSonic.TableSchema.TableColumn[] { User.UserIdColumn, User.NameColumn })
.From(User.Schema)
.WhereExpression(User.UserIdColumn.ColumnName).IsEqualTo(1).Or(User.UserIdColumn).IsEqualTo(2)
.CloseExpression()
.And(User.NameColumn).Like("a")
Gives query like:
SELECT [dbo].[Users].[UserId], [dbo].[Users].[Name]
FROM [dbo].[Users]
WHERE ([dbo].[Users].[UserId] = #UserId0 OR [dbo].[Users].[UserId] = #UserId)
AND [dbo].[Users].[Name] LIKE #Name