Oracle parameters in c# inline query with wildcard - c#

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.

Related

Generic array as parameter in method

I'm trying to convert a string[] to a more generic type. The answer is probably simple, but I can't figure it out now.
My problem is that I have a method genericMethod() that takes an SQL query as the first parameter, so it would initially look like this: genericMethod(string).
But the SQL query takes in some parameters too and that differs depending on how I need to use the SQL query.
One query can look like this:
genericMethod($#"
SELECT [CustomerId], COUNT (*) , MAX (OrderDate) FROM [MyTable]
WHERE [ProductType] = #0
AND ([OrderDate] > DATEADD(DAY, -#1, GETUTCDATE()))"
, productType, myChosenDate);
Meaning that the array-param consist of string and int.
But in another situation it would look something like this:
genericMethod($#"
SELECT [CustomerId], COUNT (*) , MAX (OrderDate) FROM [MyTable]
WHERE ([OrderDate] > DATEADD(DAY, -#1, GETUTCDATE()))"
, myChosenDate);
Meaning that the array-param only consist of an int.
And a third the parameters would be string (for the query), and then 1 to N string parameters for the query, which is the one I am using right now.
Example of the third usage:
genericMethod($#"
SELECT [CustomerId], COUNT (*) , MAX (OrderDate) FROM [MyTable]
WHERE [ProductType] = #0
WHERE [Manufacturer] = #1
AND ([OrderDate] > DATEADD(DAY, -7, GETUTCDATE()))"
, productType, manufacturer);
Hope that makes sense.
The way I've solved it so far is by using genericMethod(string, string[] params), but as you can see I want the last parameter to be more generic, so that the array can consist of both strings and int and not only strings.
Is that possible?

Split varchar value used in WHERE clause

I am passing a list of string from c# code concatenated to a varchar variable in MySQL, separated by ",".
I want to split the varchar and use it in my WHERE clause like the sample code below:
SELECT * FROM table_name WHERE column_name IN split(varchar_variable, ",")
Do mysql have function as the split function that returns an array?
Or if you have some suggestions on how to do it right, thank you in advance.
Unfortunately there is no such function.
You can do some workaround: if you have ilst of values like
value1,value2,value3
Then it's enough to look for ,value1, using LIKE operator. n order for this to work you need to pad list with commas:
,value1,value2,value3,
So your query would become:
SELECT *
FROM table_name
WHERE ',' + varchar_variable + ',' LIKE ',' + column_name + ','
If you want to pass multiple values inside WHERE clause with your SELECT statement then you need to modify your WHERE clause parameter values like
string values = "'\'Albania\',\'Andorra\''";
If you debug this code then it will look like,
''Albania','Andorra''
And in MySql editor you can pass same values like,
SET #myArray := '\'Albania\',\'Andorra\'';
So finally your SELECT statement will be,
SET #myArray := '\'Albania\',\'Andorra\'';
SET #sql = CONCAT('SELECT CountryID, CountryName FROM Countries WHERE CountryName IN (', #myArray, ')');
PREPARE statement FROM #sql;
EXECUTE statement;
Output:

Executing query with like is not working

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";

INSERT with SELECT DISTINCT and another field included?

I have a products table and each product have a category field (this is not up to me to change and I need to read the data from it as it is), from the product table I wanted to update a category table with unique categories only (I also use a IFNULL to deal with empty categories), this is what I have so far that works:
string query = "INSERT INTO categories (name)
SELECT DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name)";
With the above query I am able to update the categories with unique category fields and every time I need to run it, it will add only categories that does not exist to the table.
But besides the above I also need to include a profile id with each category inserted something like this, which does not work:
string query = "INSERT INTO categories (profile_id, name)
SELECT " + profileId.ToString() + ",
DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name
AND profile_id = #profileId)";
Is there a way I could acomplish this with a query ?
The above gives me a message:
SQLite error near "DISTINCT": syntax error
SqliteMan error:
Query Error: near "DISTINCT": syntax error Unable to execute statement
Let me know if I havent been clear and what should I be more clear about.
You are using the DISTINCT keyword incorrectly: it is not a function. Try instead:
"INSERT INTO categories (profile_id, name)
SELECT DISTINCT '"+profileId.ToString()+"', IFNULL(category, 'Uncategorized')
FROM products
WHERE category_name NOT IN (
SELECT name FROM categories WHERE profile_id = "'+profileId.ToString()+"'
)
"
One assumes that profileId.ToString() is guaranteed to be safe from SQL injection, or else you would be escaping it/passing it as a parameter to a prepared statement?

How do I execute a MySQL parameterized query in C#?

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 ?

Categories