0

ユーザー用の SQL Server データベースがあり、このテーブルの単純な検索アルゴリズムを作成したいと考えています。私の目標は、複数の単語の検索を組み合わせることができるアルゴリズムを作成することなので、この戦略で試しました: 検索ボタンが押されると、メソッドは最初に検索文字列から個々の単語を抽出し、次にそれらをパラメーターとして に挿入しますSqlCommand CommandText

1 つの単語に対しては機能しますが、複数の単語を入力すると動かなくなります。コードサンプルは次のとおりです。

private void btnSearchUsers_Click(object sender, EventArgs e)
{
             command = new SqlCommand();
             adapter = new SqlDataAdapter();
             dataset = new DataSet();
            try
            {
                User.connection.Open();
                command.CommandText = "SELECT * FROM tbl_Users WHERE userActive = 1";
                if (!String.Empty.Equals(tboxInputUsers.Text))
                {
                    command.CommandText += " AND";
                    string[] words = tboxInputUsers.Text.Split(' ');
                    string id = "id";

                    foreach (string word in words)
                    {
                        id += "a";
                        command.CommandText += String.Format(" userUsername LIKE @{0} OR userName LIKE @{0} OR userSurname LIKE @{0}", id);
                        command.Parameters.AddWithValue(String.Format("@{0}", id), word);
                    }
                }
                command.Connection = User.connection;
                adapter.SelectCommand = command;

                adapter.Fill(dataset);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (ConnectionState.Open.Equals(User.connection.State)) Korisnik.connection.Close();
            }
            DataTable table = new DataTable();
            table = dataset.Tables[0];

            dgvUsers.DataSource = table;
            dgvUsers.Columns[0].Visible = false;
}

デバッグ モードに移動して 2 つの単語を入力すると、command.Parameters に 2 つのパラメーターがあることが示されますが、それでもクラックします。何が間違っているのですか?

ありがとう!

==編集==

私はこのタイプの

SqlException: 'userUsername' 付近の構文が正しくありません

4

2 に答える 2

2

コマンドを実行する直前に出力してから、手動で実行してみてください。これは通常、SQL コマンドの構文の問題を浮き彫りにします。

于 2012-08-24T09:38:05.380 に答える
1

私はまだあなたのコードを完全に理解していませんが、一見
すると、コマンド テキストを括弧で囲み、OR 論理演算子を追加する必要があります。

command.CommandText += String.Format(" (userUsername LIKE @{0} OR userName LIKE @{0} OR userSurname LIKE @{0}) OR ", id); 

もちろん、最後のORを削除する必要があるループを終了します

command.CommandText = command.CommandText.Substring(0, command.CommandText.Length - 4);

単純な文字列を使用してコードを書き直そうとします

string sqlText = "SELECT * FROM tbl_Users WHERE userActive = 1"; 
if (!String.Empty.Equals(tboxInputUsers.Text)) 
{ 
    // It's important to add an open parenthesis here to get a correct logic
    // All activeusers with names like words
    sqlText += " AND ("; 
    string[] words = tboxInputUsers.Text.Split(' '); 
    string id = "id"; 

    foreach (string word in words) 
    { 
        id += "a"; 
        sqlText += String.Format(" (userUsername LIKE @{0} OR " + 
                                   "userName LIKE @{0} OR " + 
                                   "userSurname LIKE @{0}) OR ", id); 
        command.Parameters.AddWithValue(String.Format("@{0}", id), word); 
    } 
    // We are sure that there is at least one word, so we can  remove the excess OR and close
    // the opening parenthesis following the AND
    sqlText = sqlText(0, sqlText.Length - 4);
    sqlText += ")";
}
command.CommandText = sqlText;
于 2012-08-24T09:33:16.510 に答える