我有直接从管理工作室执行的存储过程似乎工作正常,但是当它来到vb.net代码我总是得到0.任何人都可以看看并告诉我这里缺少什么。我阅读文档,似乎应该按预期工作,但它不是以某种方式。
存储过程:
ALTER PROCEDURE [dbo].[IsPhraseConnectedAlready]
@PhraseId INT,
@KatSubkatId INT,
@WordId INT,
@SubsubkatId INT = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @temp_T_Html_Word_Categories TABLE(Id INT)
DECLARE @temp_T_Html_WordCat_Phrase TABLE(FK_Phrase_ID INT)
DECLARE @temp_T_Html_Phrase TABLE(Id INT, [Name] varchar(max))
IF @SubsubkatId IS NULL
BEGIN
INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId And FK_Subsubkat_ID IS NULL;
END
ELSE
BEGIN
INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId And FK_Subsubkat_ID = @SubsubkatId;
END
Declare @Id int;
Select @Id = Id From @temp_T_Html_Word_Categories;
INSERT INTO @temp_T_Html_WordCat_Phrase(FK_Phrase_ID) SELECT FK_Phrase_ID FROM T_Html_WordCat_Phrase WHERE FK_Word_Categorie_ID = @Id;
IF (@@ROWCOUNT > 0)
BEGIN
Declare @FK_Phrase_ID int;
--it makes no difference what you SELECT in an EXISTS sub-query as it is just syntactical sugar. (ie Nothing is actually selected.) SELECT * or SELECT 1 ...
WHILE EXISTS(SELECT * FROM @temp_T_Html_WordCat_Phrase)
BEGIN
Select Top 1 @FK_Phrase_ID = FK_Phrase_ID From @temp_T_Html_WordCat_Phrase;
INSERT INTO @temp_T_Html_Phrase(Id, [Name]) SELECT Id, [Name] FROM T_Html_Phrase WHERE Id = @FK_Phrase_ID;
IF (@PhraseId = @FK_Phrase_ID)
RETURN 1
Delete @temp_T_Html_WordCat_Phrase Where FK_Phrase_ID = @FK_Phrase_ID;
END;
END
ELSE
BEGIN
Delete @temp_T_Html_Word_Categories;
RETURN 0
END
RETURN 0
END
这是我的visual basic.net代码:
Public Function IsPhraseConnectedAlready(phraseId As Integer, katsubkatid As Integer, wordid As Integer, subsubkatid As Integer?) As Integer
Using con As New SqlConnection(_strcon)
Using cmd As New SqlCommand("IsPhraseConnectedAlready", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PhraseId", phraseId)
cmd.Parameters.AddWithValue("@KatSubkatId", katsubkatid)
cmd.Parameters.AddWithValue("@WordId", wordid)
If subsubkatid.HasValue Then 'check whether nullable field contain value
cmd.Parameters.AddWithValue("@SubsubkatId", subsubkatid.Value)
Else
cmd.Parameters.AddWithValue("@SubsubkatId", DBNull.Value)
End If
con.Open()
Dim i = CType(cmd.ExecuteScalar(), Integer)
If i = 1 Then
Return True
ElseIf i = 0 Then
Return False
End If
End Using
End Using
End Function
从ssms执行直接显示它到达RETURN 1,但是从vb.net传递相同的值我得到0:
vb.net中的使用相同的值:
似乎决议是这样做的:
而不是:
RETURN 1 or RETURN 0
将其设为:
SELECT 1 or SELECT 0
然而,当它到达第一个选择时,在每个SELECT之后添加额外的返回时断开执行。
有没有人发现任何问题?对我来说,它有效并且想知道为什么只有RETURN不是来自vb.net
答案 0 :(得分:1)
cmd.ExecuteScalar()
will execute the query, and return the first column of the first row in the result set returned by the query. However, you're not returning a column/row, you're just returning a value.
So, either convert RETURN 1
to SELECT 1
or else declare a return parameter in your VB.Net code like so:
...
Dim returnParameter As SqlParameter = cmd.Parameters.Add("@ReturnVal", SqlDbType.Int)
returnParameter.Direction = ParameterDirection.ReturnValue
con.Open()
cmd.ExecuteNonQuery()
Dim i = CType(returnParameter.Value, Integer)
...