124-c-sharp-sql-like-command-for-search

Easier way to connect to database is

In this tutorial c sharp SQL like command for search is explained, i.e. how to search similar data with SQL query command like from C#. We continue from initial database tutorial.
C sharp SQL like command for search in our example is following:

ltbxPersons.Items.Clear();
alConnection.Clear();

string str_search_what = textSearch.Text;
string str_sql_search = “select ID_PERSON, NAME, SURNAME from [PERSON] “;
str_sql_search += ” where “;
str_sql_search += ” NAME like @Search “;
str_sql_search += ” OR SURNAME like @Search “;
str_sql_search += ” order by NAME, SURNAME “;

sqlCmd.Parameters.AddWithValue(“Search”, “%” + textSearch.Text.Trim() + “%”);

sqlCn.Open();
sqlCmd.CommandText = str_sql_search;
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{

ltbxPersons.Items.Add((sqlReader[“NAME”].ToString() + ” ” + sqlReader[“SURNAME”].ToString()).Trim());
alConnection.Add(sqlReader[“ID_PERSON”]);

}
sqlCn.Close();
sqlCmd.Parameters.Clear();

External links:

C Sharp SQL like Command for search on Bytes.com
C Sharp SQL like Command for search on Abundantcode.com
C Sharp SQL like Command for search on Developerfusion.com

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*