使用 LIKE 关键字在 SQL 中搜索数据

发布于 2024-12-12 05:00:16 字数 6696 浏览 0 评论 0原文

我有一个 SQL 存储过程,它根据发送数据在我的表中搜索数据。 但当我使用它时,我无法得到任何结果。 这是我的 C# 代码,正在使用存储过程:

List<Common.CommonPersonSerchResult> SerchResult = new List<Common.CommonPersonSerchResult>();

    public DLAdvancedSearch(Common.CommonPersonAdvancedSearch data)
    {
        //Creating Connection Started...
        SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True");
        //Creating Connection Finished.
        //Creating Command To Run started...
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        //Creating Command To Run Finished.
        //Setting Command Text...
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "AdvancedSearch";
        //Setting Command Text Finished.

        //Making And Setting SQL AdvancedSearch Parametters...
        SqlParameter FirstName = new SqlParameter("FirstName", SqlDbType.NVarChar, 50);
        FirstName.Value = data.FirstName;
        command.Parameters.Add(FirstName);

        SqlParameter LastName = new SqlParameter("LastName", SqlDbType.NVarChar, 50);
        LastName.Value = data.LastName;
        command.Parameters.Add(LastName);

        SqlParameter FatherName = new SqlParameter("FatherName", SqlDbType.NVarChar, 50);
        FatherName.Value = data.FatherName;
        command.Parameters.Add(FatherName);

        SqlParameter NationalCode = new SqlParameter("NationalCode", SqlDbType.Int);
        NationalCode.Value = data.NationalCode;
        command.Parameters.Add(NationalCode);

        SqlParameter ShenasnameCode = new SqlParameter("ShenasnameCode", SqlDbType.Int);
        ShenasnameCode.Value = data.ShenasnameCode;
        command.Parameters.Add(ShenasnameCode);

        SqlParameter State = new SqlParameter("State", SqlDbType.NVarChar, 50);
        State.Value = data.State;
        command.Parameters.Add(State);

        SqlParameter City = new SqlParameter("City", SqlDbType.NVarChar, 50);
        City.Value = data.City;
        command.Parameters.Add(City);

        SqlParameter Address = new SqlParameter("Address", SqlDbType.NVarChar, 50);
        Address.Value = data.Address;
        command.Parameters.Add(Address);

        SqlParameter PostalCode = new SqlParameter("PostalCode", SqlDbType.Int);
        PostalCode.Value = data.PostalCode;
        command.Parameters.Add(PostalCode);

        SqlParameter SportType = new SqlParameter("SportType", SqlDbType.NVarChar, 50);
        SportType.Value = data.SportType;
        command.Parameters.Add(SportType);

        SqlParameter SportStyle = new SqlParameter("SportStyle", SqlDbType.NVarChar, 50);
        SportStyle.Value = data.SportStyle;
        command.Parameters.Add(SportStyle);

        SqlParameter RegisterType = new SqlParameter("RegisterType", SqlDbType.NVarChar, 50);
        RegisterType.Value = data.RegisterType;
        command.Parameters.Add(RegisterType);

        SqlParameter Gahremani = new SqlParameter("Gahremani", SqlDbType.NVarChar, 50);
        Gahremani.Value = data.Ghahremani;
        command.Parameters.Add(Gahremani);

        //Making And Setting SQL AdvancedSearch Parametters Finished.

        //Reading Data And Save in SearchResult List...
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Common.CommonPersonSerchResult res = new Common.CommonPersonSerchResult();

            res.ID = (int)reader.GetValue(0);
            res.FirstName = reader.GetValue(1).ToString();
            res.LastName = reader.GetValue(2).ToString();
            res.FatherName = reader.GetValue(3).ToString();
            res.NationalCode = (int)reader.GetValue(4);
            res.ShenasnameCode = (int)reader.GetValue(5);
            res.BirthDate = reader.GetValue(6).ToString();
            res.State = reader.GetValue(7).ToString();
            res.City = reader.GetValue(8).ToString();
            res.PostalCode = (int)reader.GetValue(10);
            res.SportType = reader.GetValue(11).ToString();
            res.SportStyle = reader.GetValue(12).ToString();
            res.RegisterType = reader.GetValue(13).ToString();
            res.Ghahremani = reader.GetValue(14).ToString();

            SerchResult.Add(res);

        }

        connection.Close();
        //Reading Data And Save in SearchResult List Finished.


    }

这是我的存储过程:

USE [Khane]
GO
/****** Object:  StoredProcedure [dbo].[AdvancedSearch]    Script Date: 10/28/2011 01:02:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[AdvancedSearch] 
@FirstName nvarchar(50) = null,
@LastName nvarchar(50) = null,
@FatherName nvarchar(50) = null,
@NationalCode int = null,
@ShenasnameCode int = null,
@State nvarchar(50) =null,
@City nvarchar(30) =null,
@Address nvarchar(250)=null,
@PostalCode int=null,
@SportType nvarchar(50)=null,
@SportStyle nvarchar(50)=null,
@RegisterType nvarchar(50)=null,
@Gahremani nvarchar(50)=null
AS
BEGIN
if @FirstName<>null
begin
select * from PersonsDataTbl where Name like '%'+@FirstName+'%'
end

if @LastName<>null
begin
select * from PersonsDataTbl where LastName like '%'+@LastName+'%'
end

if @FatherName<>null
begin
select * from PersonsDataTbl where FatherName like '%'+@FatherName+'%'
end

if @NationalCode<>null
begin
select * from PersonsDataTbl where NationalCode like '%'+@NationalCode+'%'
end

if @ShenasnameCode<>null
begin
select * from PersonsDataTbl where ShenasnameCode like '%'+@ShenasnameCode+'%'
end

if @State<>null
begin
select * from PersonsDataTbl where State like '%'+@State+'%'
end

if @City<>null
begin
select * from PersonsDataTbl where City like '%'+@City+'%'
end

if @Address<>null
begin
select * from PersonsDataTbl where Address like '%'+@Address+'%'
end

if @PostalCode<>null
begin
select * from PersonsDataTbl where PostalCode like '%'+@PostalCode+'%'
end

if @SportType<>null
begin
select * from PersonsDataTbl where SportType like '%'+@SportType+'%'
end

if @SportStyle<>null
begin
select * from PersonsDataTbl where SportStyle like '%'+@SportStyle+'%'
end

if @RegisterType<>null
begin
select * from PersonsDataTbl where RegisterType like '%'+@RegisterType+'%'
end

if @Gahremani<>null
begin
select * from PersonsDataTbl where Ghahremani like '%'+@Gahremani+'%'
end

END

我必须做什么?

I have a SQL stored procedure that is searching data in my table according to send data.
But when I use it I can't get any result.
This is my C# code hat is using the stored procedure:

List<Common.CommonPersonSerchResult> SerchResult = new List<Common.CommonPersonSerchResult>();

    public DLAdvancedSearch(Common.CommonPersonAdvancedSearch data)
    {
        //Creating Connection Started...
        SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True");
        //Creating Connection Finished.
        //Creating Command To Run started...
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        //Creating Command To Run Finished.
        //Setting Command Text...
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "AdvancedSearch";
        //Setting Command Text Finished.

        //Making And Setting SQL AdvancedSearch Parametters...
        SqlParameter FirstName = new SqlParameter("FirstName", SqlDbType.NVarChar, 50);
        FirstName.Value = data.FirstName;
        command.Parameters.Add(FirstName);

        SqlParameter LastName = new SqlParameter("LastName", SqlDbType.NVarChar, 50);
        LastName.Value = data.LastName;
        command.Parameters.Add(LastName);

        SqlParameter FatherName = new SqlParameter("FatherName", SqlDbType.NVarChar, 50);
        FatherName.Value = data.FatherName;
        command.Parameters.Add(FatherName);

        SqlParameter NationalCode = new SqlParameter("NationalCode", SqlDbType.Int);
        NationalCode.Value = data.NationalCode;
        command.Parameters.Add(NationalCode);

        SqlParameter ShenasnameCode = new SqlParameter("ShenasnameCode", SqlDbType.Int);
        ShenasnameCode.Value = data.ShenasnameCode;
        command.Parameters.Add(ShenasnameCode);

        SqlParameter State = new SqlParameter("State", SqlDbType.NVarChar, 50);
        State.Value = data.State;
        command.Parameters.Add(State);

        SqlParameter City = new SqlParameter("City", SqlDbType.NVarChar, 50);
        City.Value = data.City;
        command.Parameters.Add(City);

        SqlParameter Address = new SqlParameter("Address", SqlDbType.NVarChar, 50);
        Address.Value = data.Address;
        command.Parameters.Add(Address);

        SqlParameter PostalCode = new SqlParameter("PostalCode", SqlDbType.Int);
        PostalCode.Value = data.PostalCode;
        command.Parameters.Add(PostalCode);

        SqlParameter SportType = new SqlParameter("SportType", SqlDbType.NVarChar, 50);
        SportType.Value = data.SportType;
        command.Parameters.Add(SportType);

        SqlParameter SportStyle = new SqlParameter("SportStyle", SqlDbType.NVarChar, 50);
        SportStyle.Value = data.SportStyle;
        command.Parameters.Add(SportStyle);

        SqlParameter RegisterType = new SqlParameter("RegisterType", SqlDbType.NVarChar, 50);
        RegisterType.Value = data.RegisterType;
        command.Parameters.Add(RegisterType);

        SqlParameter Gahremani = new SqlParameter("Gahremani", SqlDbType.NVarChar, 50);
        Gahremani.Value = data.Ghahremani;
        command.Parameters.Add(Gahremani);

        //Making And Setting SQL AdvancedSearch Parametters Finished.

        //Reading Data And Save in SearchResult List...
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Common.CommonPersonSerchResult res = new Common.CommonPersonSerchResult();

            res.ID = (int)reader.GetValue(0);
            res.FirstName = reader.GetValue(1).ToString();
            res.LastName = reader.GetValue(2).ToString();
            res.FatherName = reader.GetValue(3).ToString();
            res.NationalCode = (int)reader.GetValue(4);
            res.ShenasnameCode = (int)reader.GetValue(5);
            res.BirthDate = reader.GetValue(6).ToString();
            res.State = reader.GetValue(7).ToString();
            res.City = reader.GetValue(8).ToString();
            res.PostalCode = (int)reader.GetValue(10);
            res.SportType = reader.GetValue(11).ToString();
            res.SportStyle = reader.GetValue(12).ToString();
            res.RegisterType = reader.GetValue(13).ToString();
            res.Ghahremani = reader.GetValue(14).ToString();

            SerchResult.Add(res);

        }

        connection.Close();
        //Reading Data And Save in SearchResult List Finished.


    }

And this is my stored procedure:

USE [Khane]
GO
/****** Object:  StoredProcedure [dbo].[AdvancedSearch]    Script Date: 10/28/2011 01:02:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[AdvancedSearch] 
@FirstName nvarchar(50) = null,
@LastName nvarchar(50) = null,
@FatherName nvarchar(50) = null,
@NationalCode int = null,
@ShenasnameCode int = null,
@State nvarchar(50) =null,
@City nvarchar(30) =null,
@Address nvarchar(250)=null,
@PostalCode int=null,
@SportType nvarchar(50)=null,
@SportStyle nvarchar(50)=null,
@RegisterType nvarchar(50)=null,
@Gahremani nvarchar(50)=null
AS
BEGIN
if @FirstName<>null
begin
select * from PersonsDataTbl where Name like '%'+@FirstName+'%'
end

if @LastName<>null
begin
select * from PersonsDataTbl where LastName like '%'+@LastName+'%'
end

if @FatherName<>null
begin
select * from PersonsDataTbl where FatherName like '%'+@FatherName+'%'
end

if @NationalCode<>null
begin
select * from PersonsDataTbl where NationalCode like '%'+@NationalCode+'%'
end

if @ShenasnameCode<>null
begin
select * from PersonsDataTbl where ShenasnameCode like '%'+@ShenasnameCode+'%'
end

if @State<>null
begin
select * from PersonsDataTbl where State like '%'+@State+'%'
end

if @City<>null
begin
select * from PersonsDataTbl where City like '%'+@City+'%'
end

if @Address<>null
begin
select * from PersonsDataTbl where Address like '%'+@Address+'%'
end

if @PostalCode<>null
begin
select * from PersonsDataTbl where PostalCode like '%'+@PostalCode+'%'
end

if @SportType<>null
begin
select * from PersonsDataTbl where SportType like '%'+@SportType+'%'
end

if @SportStyle<>null
begin
select * from PersonsDataTbl where SportStyle like '%'+@SportStyle+'%'
end

if @RegisterType<>null
begin
select * from PersonsDataTbl where RegisterType like '%'+@RegisterType+'%'
end

if @Gahremani<>null
begin
select * from PersonsDataTbl where Ghahremani like '%'+@Gahremani+'%'
end

END

What do I have to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-12-19 05:00:16

您应该使用 ISIS NOT 来与 NULL 进行比较,而不是 =<>

if @FirstName IS NOT NULL

Instead of = and <> you should use IS and IS NOT to compare to NULL:

if @FirstName IS NOT NULL
对风讲故事 2024-12-19 05:00:16

您需要在这里隔离您的问题。你需要问自己的第一个问题

我的存储过程是否按预期工作?

停止查看 C# 代码,直到确定存储过程可以工作。一旦您知道该问题的答案,您将有两个后续问题之一:

  1. 为什么我的存储过程不起作用?
  2. 为什么我无法从 C# 应用程序成功调用工作存储过程?

学会如何排查此类问题是找到问题的关键。据我们所知,您的连接字符串可能不正确。

You need to isolate your problem here. The first question you need to ask yourself

Does my stored procedure work as expected?

Stop looking at your C# code until you have determined that the stored procedures works. Once you know the answer to that question, you'll have one of two follow-up questions:

  1. Why doesn't my stored procedure work?
  2. Why can't I call my working stored procedure successfully from my C# application?

Learning how to troubleshoot this type of problem is the key to finding the problem. For all we know your connection string might be incorrect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文