現在、コードを少しクリーンアップし、VSからSqlParameter
、複合語の代わりにsqlコマンドにforを使用する方がよいと言われましたstring
。そこで、コードを変更することにしました。残念ながら、結果が得られず、理由もわかりません。これが私のコードの一部です:
...
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetSQLConnectionString());
SqlDataAdapter sqlSelect = new SqlDataAdapter();
try
{
connection.Open();
sqlSelect.SelectCommand = connection.CreateCommand();
sqlSelect.SelectCommand.CommandText = "SELECT id, @FROM AS \"from\", @TO AS \"to\" FROM Dictionary WHERE @FROM LIKE @SEARCHSTRING";
sqlSelect.SelectCommand.Parameters.Add(new SqlParameter("@FROM", this.from));
sqlSelect.SelectCommand.Parameters.Add(new SqlParameter("@TO", this.to));
sqlSelect.SelectCommand.Parameters.Add(new SqlParameter("@SEARCHSTRING", "'%" + this.SearchField.Text + "%'"));
sqlSelect.Fill(dt);
connection.Close();
}
catch(SqlException e)
...
例外はありません。検索後にdtが空になるのはなぜですか?(複合文字列を使用すると、選択が機能します。)何が問題でしたか?
グリーツ