検索テキストボックスに入力された値と銀行に類似または同等の値を取得したい。例。
出演:G.
戻り値: G を含むすべての名前。
入力する人がジョバンニと書いてください。ジョバンニだけが返されます。
私のコードですが、同じものを返すだけです
[...]"select * from users where name = " + txt_name.Text, conn);
試す:
"SELECT * FROM users WHERE name LIKE '" + txt_name.Text +"%'"
ちなみに、Parameters
あなたのコードは次のようなSqlインジェクションの影響を受けやすいため、使用してください:(私はここでSqlを使用しているので、mySql Connection、Adapterなどに変更してください)
using (SqlConnection connection = new SqlConnection(connString))
{
string sql = "SELECT * FROM users WHERE name LIKE @NameSearch";
try
{
connection.Open();
using (SqlDataAdapter da = new SqlDataAdapter(sql, connection))
{
using (SqlCommand cmd = new SqlCommand())
{
da.SelectCommand.Parameters.AddWithValue("@NameSearch", txt_Name.Text+"%");
DataTable dt = new DataTable();
da.Fill(dt);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
SELECT names FROM table WHERE name LIKE '[your text]%'
次のようなワイルド文字検索を使用します
テキストを含むすべての名前が必要な場合は、次を使用します。
Select names from table where name like '%[your text]%'
テキストで始まるすべての名前が必要な場合は、次を使用します。
Select names from table where name like '[your text]%'
テキストで終わるすべての名前が必要な場合は、次を使用します。
Select names from table where name like '%[your text]%'
LIKE
そのための演算子を試してください:
SELECT * FROM users WHERE name LIKE 'G%'