搜索功能无法完全发挥作用

时间:2019-01-19 20:35:56

标签: c# sql-server stored-procedures

我已经在SQL Server中创建了一个存储过程:

ALTER PROCEDURE [dbo].[People_SearchByValue]
    @strFind AS NVARCHAR(MAX)    
AS
/*
EXEC People_SearchByValue @strFind = m
*/
BEGIN
    SET NOCOUNT ON;

    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE Title Like @strFind
    END
    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE Bio Like @strFind
    END
    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE Headline Like @strFind
    END
    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE Summary Like @strFind
    END
    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE Slug Like @strFind
    END
    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE StatusId Like @strFind
    END
    BEGIN
        SELECT Title, Bio, Headline, Summary, Slug, StatusId, Skills 
        FROM PeopleTable
        WHERE Skills Like @strFind
    END
END

基本上,它使我可以设置搜索变量,并将在其一列中返回带有该变量的人。

但是,当我在C#中调用此过程时,它完全可以正常工作,但仅当我搜索“标题”输入时,才对其他任何列均无效。

namespace TestingOnceAgain
{
    class Program
    {
        static void Main(string[] args)
        {
            var command = new SqlCommand("People_SearchByValue");
            command.CommandType = CommandType.StoredProcedure;

            Console.WriteLine("What are you searching for?");
            command.Parameters.Add("@strFind", SqlDbType.NVarChar, 100).Value = Console.ReadLine();

            using (SqlConnection PubsConn = new SqlConnection("Server=.\\SQLEXPRESS;Database=PeopleDatabase;Trusted_Connection=True;"))
            {
                PubsConn.Open();
                command.Connection = PubsConn;
                SqlDataReader myReader = command.ExecuteReader();

                while (myReader.Read())
                {
                    Console.WriteLine("Title: {0} Bio: {1} Headline: {2} Summary: {3} Slug: {4} StatusId: {5} Skills: {6}", 
                        myReader.GetString(0), 
                        myReader.GetString(1), 
                        myReader.GetString(2), 
                        myReader.GetString(3),
                        myReader.GetString(4),
                        myReader.GetSqlInt32(5),
                        myReader.GetString(6));
                }              
            }

            Console.ReadLine();
        }
    }
}

如果我尝试在bio或任何其他列中搜索字段,则不会写入任何内容。

0 个答案:

没有答案