2017-01-05 6 views
2

このコードでは、検索フォームのテキストボックスに指定された値に基づいて、データを検索してDataGridViewにロードできます。テキストボックスを空白のままにしておくと、SQLクエリが「AND」と結合されているため、検索結果は表示されません。データベースで検索中に空のテキストボックスを無視する

(SQLクエリまたはC#コードから)検索中に空のテキストボックスを無視するにはどうすればよいですか?

private void btnSearch_Click(object sender, EventArgs e) 
{ 
    DataSet ds = new DataSet(); 

    String select = "SELECT DocumentNo, Revision, DocumentTitle, DocumentType 
        FROM DocumentLog 
        WHERE DocumentNo Like '%" + tbxDocumentNo.Text + "%' 
         AND Revision Like '%" + tbxRevision.Text + "%' 
         AND DocumentTitle Like '%" + tbxDocumentTitle.Text + "%' 
         AND DocumentType '%" + tbxDocumentType.Text + "%'" 
         AND IsDeleted = '0'; 

    SqlConnection conn = DBConnection.openConnection(); 
    SqlCommand command = new SqlCommand(select, conn); 

    SqlDataAdapter da = new SqlDataAdapter(command); 
    da.Fill(ds, "DocumentLog"); 

    dgvTracking.AutoGenerateColumns = false; 
    dgvTracking.DataSource = ds.Tables["DocumentLog"]; 
} 
+3

バインド・パラメータを使用してください、あなたのコードは、SQLインジェクション攻撃に対して脆弱です。 – nvoigt

+0

クエリ文字列の外側に 'AND IsDeleted = '0''が表示されるのはなぜですか? – LightBulb

答えて

3
  • まず、
  • 第二に、あなたは値が前にどこにそれらを追加nullまたは空であるかどうかを確認するためにString.IsNullOrEmptyを使用することができます句。
  • 通知する必要があるのは、複数の条件の区切り文字がANDである場合です。追加すると、2番目の条件の先頭(tbxRevision)には、tbxDocumentNoがnullまたは空の場合にクエリがエラーになります。最後の条件が偽である場合は最後に同様です。クエリはANDで終了しますが、これもエラーです。これらを避けるには、
のように最初の条件として IsDeleted='0'を使用します。

を見てください:

string querySQL = "Select DocumentNo , Revision, DocumentTitle, DocumentType FROM DocumentLog WHERE IsDeleted='0'"; 
using(SqlConnection conSQL = DBConnection.openConnection()) 
{ 
    using(SqlCommand cmdSQL = new SqlCommand()) 
    { 
     if(!string.IsNullOrEmpty(tbxDocumentNo.Text)) 
     { 
      querySQL += "AND DocumentNo Like @DocumentNo"; 
      cmdSQL.Parameters.Add("@DocumentNo", SqlDbType.VarChar).Value = "%" + tbxDocumentNo.Text + "%"; 
     } 

     // Add rest of conditions here like this 

     cmdSQL.CommandText=querySQL; 
     cmdSQL.Connection = conSQL; 
    SqlDataAdapter da = new SqlDataAdapter(cmdSQL);          
    } 
} 
+0

1 = 1ではなく、常にハードコードされたIsDeleted = 0を不正行為させて使用することができます。 – nvoigt

+0

@nobigt:ありがとうございます。とにかく私は答えを更新しました –

+0

ありがとう!!!御時間ありがとうございます。この作品 – ceranda

0

こんにちはあなたは、たとえば、クエリは可能性があり、このためのいくつかのクエリロジックを使用することができます。

Select DocumentNo , Revision, DocumentTitle, DocumentType 
FROM DocumentLog 
WHERE (DocumentNo Like '%"+tbxDocumentNo.Text+"%' OR tbxDocumentNo.Text = '') AND 
(Revision Like '%"+tbxRevision.Text+"%' OR Revision = '') AND 
(DocumentTitle Like '%"+tbxDocumentTitle.Text+"%' OR DocumentTitle = '') AND 
DocumentType '%"+tbxDocumentType.Text+"%'";AND IsDeleted='0' 
+0

これがどのように役立っているのかわかりません。フィールドの値が指定されていない場合、OPはフィールドの句を省略する必要があります。 – nvoigt

1

あなたのSQLコマンドを使用すると、停止する必要がありますAND Revision Like ''句を含めない場合SQLコマンドを単一の文字列としてハードコーディングし、入力ボックスに応じてその文字列を構築します。あなたは、注射を避けるために、パラメータ化クエリを連結した文字列のクエリを置き換える必要があり、すべての

StringBuilder sqlCommandText = new StringBuilder(); 

sqlCommandText.Append("Select DocumentNo , Revision, DocumentTitle, DocumentType FROM DocumentLog WHERE IsDeleted = 0"); 

if(!string.IsNullOrEmpty(tbxRevision.Text)) 
{ 
    sqlCommandText.Append(" AND Revision Like @revision"); 
    command.Parameters.Add("@revision", tbxRevision.Text); 
} 

// do this for all fields 

command.CommandText = sqlCommandText.ToString(); 
関連する問題