2011-10-27 2 views
2

데이터 전송에 따라 테이블에서 데이터를 검색하는 SQL 저장 프로 시저가 있습니다. 그러나 그것을 사용할 때 어떤 결과도 얻을 수 없습니다. 이 내 C# 코드 모자가 저장 프로 시저 사용된다LIKE 키워드를 사용하여 SQL에서 데이터 검색

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 '%'[email protected]+'%' 
end 

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

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

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

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

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

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

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

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

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

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

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

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

END 

은 내가 무엇을해야합니까?

+0

@FirstName에 전달할 매개 변수는 무엇이며 해당 테이블에 실제로 일치하는 데이터가 있다는 것이 긍정적입니까? –

답변

6
대신 =

<> 당신은 NULL로 비교 ISIS NOT을 사용해야합니다

if @FirstName IS NOT NULL 
+0

고맙습니다. –

3

당신은 사용자의 문제를 분리해야합니다. 질문해야 할 첫 번째 질문

내 저장 프로 시저가 예상대로 작동합니까?

저장 프로 시저가 작동 할 때까지 C# 코드를 살펴 보지 마십시오.

  1. 왜 저장하지 않는 프로 시저 작업 : 당신은 그 질문에 대한 답을 알고 나면, 당신은 두 가지 후속 질문 중 하나를해야합니다?
  2. 왜 C# 응용 프로그램에서 내 작업 저장 프로 시저를 성공적으로 호출 할 수 없습니까?

이 유형의 문제를 해결하는 방법을 배우는 것이 문제를 찾는 핵심입니다. 귀하의 연결 문자열이 정확하지 않을 수도 있습니다.

+0

알아요.하지만 SQL에 대한 많은 데이터가 없으며 SQL 코드에 많은 버그가 있습니다. –

관련 문제