如何从c#中的存储过程中读取输出值?

时间:2015-11-25 10:59:34

标签: stored-procedures

这是我的存储过程:

ALTER PROCEDURE sp_searchupdate
(
@id int,
@id_student int,
@output varchar(50) output,
@Tamil Varchar (100),
@English varchar (50),
@Maths Varchar (50),
@Science Varchar (50),
@SocialScience Varchar (50) 
)

AS
IF EXISTS (SELECT * FROM studentresult WHERE id=@id) 
BEGIN
UPDATE studentresult SET Tamil = @Tamil,English = @English, Maths = @Maths,Science = @Science,SocialScience = @SocialScience WHERE id = @id
SET @output='Updated'
RETURN
END
Else
BEGIN
IF EXISTS (SELECT * FROM student WHERE id=@id_student)
SET @output='EXIST'
RETURN
BEGIN
INSERT into studentresult (id_student,Tamil,English,Maths,Science,SocialScience) values (@id_student,@Tamil,@English,@Maths,@Science,@SocialScience)
SET @output='Inserted'
RETURN
END
END

如果在前端插入数据,则需要显示'插入'或更新意味着更新'否则就会存在'。

但是上述查询没有显示通知消息。

我可以知道,我的代码中的错误是什么?

任何人都可以指导我,我是新的存储过程和.net。

谢谢,

1 个答案:

答案 0 :(得分:0)

相当不清楚你想要达到的目标 - 如果学生记录不存在,为什么你只会插入到学生记录中?无论如何,目前如果没有学生成绩记录,那么在检查学生记录后,程序将始终返回。在IF EXISTS之后没有BEGIN ... END,因此IF之后的第一个语句将取决于结果。尝试:

ALTER PROCEDURE sp_searchupdate
(
@id int,
@id_student int,
@output varchar(50) output,
@Tamil Varchar (100),
@English varchar (50),
@Maths Varchar (50),
@Science Varchar (50),
@SocialScience Varchar (50) 
)

AS
BEGIN
IF EXISTS (SELECT * FROM studentresult WHERE id=@id) 
BEGIN
UPDATE studentresult SET Tamil = @Tamil,English = @English, Maths =     @Maths,Science = @Science,SocialScience = @SocialScience WHERE id = @id
SET @output='Updated'
END
Else
BEGIN
IF EXISTS (SELECT * FROM student WHERE id=@id_student)
BEGIN
    SET @output='EXIST'
END
ELSE
BEGIN
    INSERT into studentresult (id_student,Tamil,English,Maths,Science,SocialScience) values (@id_student,@Tamil,@English,@Maths,@Science,@SocialScience)
    SET @output='Inserted'
END
END

RETURN
END