0

我有用户的 SQL Server 数据库,现在我想为这个表做简单的搜索算法。我的目标是制作可以组合搜索多个单词的算法,所以我尝试了这种策略:当按下搜索按钮时,方法首先从搜索字符串中提取单个单词,然后将它们作为参数插入到SqlCommand CommandText.

它适用于一个单词,但当我输入多个单词时它会卡住。这是代码示例:

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;
}

当我进入调试模式并输入两个单词时,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 回答