2014-07-11 4 views
-1

코드의 어느 부분이 잘못되었는지 알 수 없습니다.저장 프로 시저에 매개 변수 보내기

이 내 저장 프로 시저

ALTER PROCEDURE dbo.AddNewNews 
(@title nvarchar(50), 
@text ntext, 
@year int, 
@month int, 
@day int, 
@id_writer int , 
@id_subject1 int, 
@id_subject2 int, 
@id_subject3 int, 
@id_subject4 int, 
@id_subject5 int) 
AS 
    SET NOCOUNT ON 

    BEGIN TRANSACTION; 

    DECLARE @last_id_news int 
    BEGIN TRY 

     insert into news(title, text, year, month, day, id_writer) 
     values(@title, @text, @year, @month, @day, @id_writer) 

     set @last_id_news = SCOPE_IDENTITY() 

if @id_subject1 is not null 
begin 
    insert into news_subject (id_news,id_subject) values (@last_id_news,@id_subject1) 
end 
if @id_subject2 is not null 
begin 
    insert into news_subject (id_news,id_subject) values (@last_id_news,@id_subject2) 
end 
if @id_subject3 is not null 
begin 
    insert into news_subject (id_news,id_subject) values (@last_id_news,@id_subject3) 
end 
if @id_subject4 is not null 
begin 
    insert into news_subject (id_news,id_subject) values (@last_id_news,@id_subject4) 
end 
if @id_subject5 is not null 
begin 
    insert into news_subject (id_news,id_subject) values (@last_id_news,@id_subject5) 
end 

    COMMIT TRANSACTION; 
END TRY 
BEGIN CATCH 

ROLLBACK TRANSACTION; 
END CATCH; 

RETURN 

이며, 이것은 내가 내 C# 코드에서 저장 프로 시저에 매개 변수를 전송하는 방법이다 : 널 (null)이 될 수

public bool AddNewNews(News news, List<object> subject) 
{ 
    SqlParameter[] parameters = new SqlParameter[] 
    { 
     new SqlParameter ("@title", news.Id_news), 
     new SqlParameter ("@text", news.Text), 
     new SqlParameter ("@year", news.Year), 
     new SqlParameter ("@month", news.Month), 
     new SqlParameter ("@day", news.Day), 
     new SqlParameter ("@id_writer", news.Id_writer), 
     new SqlParameter ("@id_subject1", subject[0]), 
     new SqlParameter ("@id_subject2", subject[1]), 
     new SqlParameter ("@id_subject3", subject[2]), 
     new SqlParameter ("@id_subject4", subject[3]), 
     new SqlParameter ("@id_subject5", subject[4]) 
    }; 
    return SqlDBHelper.ExecuteNonQuery("AddNewNews", CommandType.StoredProcedure, parameters); 
} 

그냥 주제 [I]

그러나 저장 프로 시저로 이동하여 id_subject이 거짓인지 또는 다른 코드 부분인지 확인하지 못합니까?

+1

당신이 그것을 잘못에 대해 어떻게 생각 하죠? 구문 오류 또는 예외가 발생합니까? –

+0

어떤 오류가 나타 납니까? 마지막 리턴에 중단 점을 두어 모든 매개 변수 값 –

+0

을 볼 수 있습니다. 오류입니다. 프로 시저 또는 함수 'AddNewNews'에 제공되지 않은 '@ id_subject3'매개 변수가 필요합니다. – nimaSadeghpour

답변

3
기본값을하기 위해 저장된 프로 시저에 매개 변수를 변경

: 내가 제대로 이해한다면, 그 subject[i] 값이 null을 할 수

@id_subject1 int = null, 
@id_subject2 int = null, 
@id_subject3 int = null, 
@id_subject4 int = null, 
@id_subject5 int = null 
+0

그가 null 값을 전달하면 값은 저장된 proc로 보내지지 않습니다? –

+0

@ NicolasR NULL 값이 전달되면 매개 변수는 무시됩니다. – JBrooks

+0

알았어, 고마워, 고마워! –

1

"just the subject[i] can be null but i don't know when it goes to the store procedure and check id_subject is false or other part of code?"

. null 값을 전달하면 값이 올바르게 전달되지 않습니다.

대신,이처럼 저장 프로 시저에 매개 변수를 보내

new SqlParameter ("@id_subject3",subject[2] ?? DBNull.Value) 
관련 문제