1

I have the next code(found here at stack overflow):

 string [] arr = {"One","Two","Three"};
   var target = "One";
   var results = Array.FindAll(arr, s => s.Equals(target));

This code good for search string on array... i need to find string in sql column.

Let's say i have table ("Names"), and i want to find "Jhon".. how can i do that?

  • I don't need connectionstring or the whole method, that's i know to do, but i can't think on method to search specific string at sql table.

  • Will be great to see version of search: "Jh" and it will find "Jhon" if is there...

0

2 Answers 2

2

Well to avoid sql injection if target is user provided

string connectionString= ...
string target="jh";

using (var conn=new SqlConnection(connectionString)) {
conn.Open();
  using (var cmd=conn.CreateCommand()) {
    cmd.CommandText="select Name from Names where Name like '%'+@value+'%'";
    cmd.Parameters.AddWithValue("@value",target);

    using (var reader=cmd.ExecuteReader()) {
        while (reader.Read()) { 
            Console.WriteLine(reader[0]);
        }
    }

  }
}
  • Use like '%'+@value+'%' for contains
  • Use like @value+'%' for starts with
  • Use like '%'+@value for ends with
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT NAME
FROM NAMES
WHERE NAME='Jhon'

Is this what you're looking for?

If only a part of it needs to match:

...
WHERE NAME LIKE 'Jh%'

LIKE

1 Comment

Probably a like clause would be more useful - SELECT Name FROM Names WHERE Name like '%Jhon%'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.