Can anyone give me a solution for my problem?
I have a string array of max length. I want to compare all of my string array elements with a single SQL query. How can I do that?
string[] new = searchtext;
select Qid from questions where qdescriptions like string[0],string[1],string[2]
The string array length is not fixed, it's dynamic.
Ex: my search string is "admin login error"
Then I split that into
admin
login
error
as three parts. My expected result should contain all these three strings in database
Like this
Admin post this;
password change for login;
the error database;
Hope you understand. The results should contain all my search strings in a single search query..
C# code:
public void searchdetails(string[] searchwords) {
SqlConnection con = new SqlConnection();
con.ConnectionString = connection; con.Open();
string[] soldesc = searchwords;
int i = 0;
if (soldesc.Length == 1) {
string query1 = "select Qid from Questions where Qdescription like '% " + soldesc[i] + " %'";
}
SqlCommand cmds = new SqlCommand(query1, con); cmds.ExecuteNonQuery();
You can do this by dynamically creating your sql query:
Write a for loop in your application that loops through your search array: Pseudo code incoming:
Note: this is pseudo code, seeing as you didn't say what programming language etc you are using I can't tell you what the actual syntax for the for loop will be like or how to protect your query from sqlInjection
Your C# code should look something like this: