c# - 对多个表执行全文搜索

标签 c# asp.net sql

我在我的 asp.net 站点中实现了全文搜索,该搜索在搜索一张表时有效。但是,我希望用户能够同时搜索两个完全不同的表。我正在尝试使用以下代码:

  public List<Article> Search(List<string> keywords)
    { 
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.Append("select [aName],[aDesc] from [Table1]  union select [bName],[bDesc] from [Table2] where");

        foreach (string item in keywords)
        {
            sqlBuilder.AppendFormat("([bName] like '%{0}%' or [bDesc] like '%{0}%') and ", item);
        }


       //foreach (string item in keywords)
        //{
            //sqlBuilder.AppendFormat("([aName] like '%{0}%' or [aDesc] like '%{0}%') and    ", item);
       //}


        string sql = sqlBuilder.ToString(0, sqlBuilder.Length - 4);
        return QueryList(sql);

    }

此代码始终显示第一个表中的所有记录,并且仅对第二个表执行搜索。现在这显然是因为我在 sql 语句中没有第一个表的“where”。我不知道如何使用不同的“foreach”循环来实现每个表的“where”。有什么建议吗?

最佳答案

UNION 将连接两个不同查询的结果。并集在每个查询执行完毕后应用,因此您需要两个 WHERE 子句:

select [aName],[aDesc] from [Table1]
where ([aName] like '%{0}%' or [aDesc] like '%{0}%')

union

select [bName],[bDesc] from [Table2]
where ([bName] like '%{0}%' or [bDesc] like '%{0}%')

代码中最简单的实现涉及分别构建两个查询,然后将它们连接在一起:

StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.Append("select [aName],[aDesc] from [Table1] where ");
foreach (string item in keywords)
{
    sqlBuilder.AppendFormat(
        "([aName] like '%{0}%' or [aDesc] like '%{0}%') and ", item);
}

// That last "AND" requires a boolean statement to follow
// 1=1 will always return true and thus will not affect
// the result of your WHERE clause.
sqlBuilder.Append("1 = 1 ");

sqlBuilder.Append("UNION select [bName],[bDesc] from [Table2] where ");
foreach (string item in keywords)
{
    sqlBuilder.AppendFormat(
        "([bName] like '%{0}%' or [bDesc] like '%{0}%') and ", item);
}

foreach 循环的替代方案:

sqlBuilder.Append("select [aName],[aDesc] from [Table1] where ");
sqlBuilder.Append(
    string.Join(
        " and ",
        keywords.Select( k => string.Format( 
            "([aName] like '%{0}%' or [aDesc] like '%{0}%')", k )
        .ToArray()
    )
)

sqlBuilder.Append("UNION select [bName],[bDesc] from [Table2] where ");
sqlBuilder.Append(
    string.Join(
        " and ",
        keywords.Select( k => string.Format( 
            "([bName] like '%{0}%' or [bDesc] like '%{0}%')", k )
        .ToArray()
    )
)

但请注意,这将是一个极其效率低下的查询。如果您要搜索的行数超过数百行,我强烈建议您考虑其他方法。

此外,您似乎容易受到 SQL Injection attacks 的攻击。除非您已经事先手动清理输入,否则您应该考虑 protecting yourself .

[[A woman is talking on the phone, holding a cup]] Phone: Hi, this is your son's school. We're having some computer trouble. Mom: Oh dear—did he break something? Phone: In a way— Phone: Did you really name your son "Robert'); DROP TABLE Students;--" ? Mom: Oh, yes. Little Bobby Tables, we call him. Phone: Well, we've lost this year's student records. I hope you're happy. Mom: And I hope you've learned to sanitize your database inputs. {{title-text: Her daughter is named Help I'm trapped in a driver's license factory.}}

关于c# - 对多个表执行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277957/

相关文章:

c# - 如何使用命名空间端点获取带有死信消息的 azure 队列列表?

c# - kentico cms中自定义字段的级联下拉列表

SQL 服务器 : How to update table based on subquery in where clause?

mysql - SQL,通过 ALTER TABLE in 1 Statement 添加作为外键的列

c# - 如何对字符串列表的列表进行排序

c# - 使用 linq 选择最小值

c# - C# 合并运算符的原子性

asp.net - 在 ASP.NET MVC 4 中,子文件夹中的 _viewStart 文件是附加的还是单独的?

c# - 防止回发 vb 和 javascript

sql - 如何找到树中最新节点的不同根节点(保存在闭包表中)?