1

したがって、「装飾」、「金属」、「紙」などの文字列を含む arr という ArrayList を取得しました。

私がやりたいのは、その ArrayList をループして、データベースからデータを取得するためのクエリ文字列に各タグを追加することです。現在、私は次のようなものを持っています:

String strSel="select * from Table1 where Tags";

for(int x=0;x<arr.Count;x++){
  if (x == arr.Count - 1)
    {
      strSel += " like '%'"+arr[x]+"'%'";
    }
      else
    {
      strSel += " like '%'" + arr[x] + "'%' or Tags";
    }
}

cmdSel=new SqlCommand(strSel,connectionName);
sqlDataReaderName=cmdSel.ExecuteReader();

とにかく、「blaの近くの構文が正しくありません」というエラーが表示されます...おそらく単一引用符またはワイルドカードと関係がありますが、わかりません。私は何を間違っていますか?

4

2 に答える 2

2

前後の余分な一重引用符を削除する必要がありますpercent symbol

strSel += " like '%" + arr[x] + "%'";

エラーがスローされた理由は、クエリが次のように形成されているためです

select * from Table1 where Tags like '%'hello'%'
                                       ^     ^ extra single quote that
                                               should be removed
于 2012-11-27T00:08:29.813 に答える
0

はい、Kuya John は正しい問題を引き起こしています。

        String strSel = "select * from Table1 where Tags";
        string[] arr = new string[] { "This", "That", "Something" };
        for (int x = 0; x < arr.Count(); x++)
        {
            if (x == arr.Count() - 1)
            {
                strSel += string.Format(" like '%{0}%' ", arr[x]);
            }
            else
            {
                strSel += string.Format(" like '%{0}%' or Tags", arr[x]);
            }
        }

これは余分な ' がなくても問題なく動作します

于 2012-11-27T00:17:20.323 に答える