文字を検索するときに文字列Microsoftが検索されない理由o
は、条件が開始文字に基づいてフィルター処理されるためです。
この条件、
... WHERE [identifier] LIKE 'o%'
character で始まるすべての文字列を教えてo
ください。contains
その文字列を検索したい場合はo
、 で囲みます%%
。例、
... WHERE [identifier] LIKE '%o%'
補足として、回避する値をパラメーター化する必要がありますSQL Injection
。
次のコード スニペットを試してください。
string connStr = "connection string here";
string content = string.Format("{0}{1}{0}", "%", identifier);
string sqlStatement = "SELECT [identifier] FROM [information] WHERE [identifier] LIKE ?";
using (SqlConnection conn = new SqlConnection(connStr))
{
using(SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = sqlStatement;
comm.CommandType = CommandType.Text;
comm.Parameters.AddWithValue("@content", content);
try
{
conn.Open();
// other codes
}
catch(SqlException e)
{
// do something with the exception
// do not hide it
// e.Message.ToString()
}
}
}
適切なコーディングのために
using
オブジェクトを適切に処分するためにステートメントを使用する
try-catch
ブロックを使用して例外を適切に処理する