从 SQL 服务器返回 VB.NET 中的布尔值
Returning boolean from SQL Server in VB.NET
目前我在 SQL 服务器上有一个功能,它执行基本检查和 returns 1
(SQL 服务器数据类型 'bit') 如果为真,0
(SQL 服务器数据类型 'bit') 如果为假
这是我目前拥有的:
Public Shared Function GetIsBespoke(ByVal ProductId As Integer)
Dim Res As Boolean = False
Dim obj_SqlConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnectionString").ConnectionString)
Dim obj_SqlCommand As New SqlCommand("[TBL].[fncIsBespoke]", obj_SqlConnection)
obj_SqlCommand.CommandType = CommandType.StoredProcedure
Dim ProductId_SqlParameter As New SqlParameter("@ProductId", SqlDbType.Int)
ProductId_SqlParameter.Direction = ParameterDirection.Input
ProductId_SqlParameter.Value = ProductId
Dim Result_SqlParameter As New SqlParameter("@Result", SqlDbType.Bit)
Result_SqlParameter.Direction = ParameterDirection.ReturnValue
obj_SqlCommand.Parameters.Add(ProductId_SqlParameter)
obj_SqlCommand.Parameters.Add(Result_SqlParameter)
If Not IsDBNull(Result_SqlParameter.Value) Then
Res = Result_SqlParameter.Value
ElseIf IsDBNull(Result_SqlParameter.Value) Then
Res = False
End If
Return Res
End Function
USE [SRV]
GO
/****** Object: UserDefinedFunction [TBL].[fncIsBespoke] Script Date: 29/10/2019 2:46:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [TBL].[fncIsBespoke](@ProductID int)
RETURNS bit
AS
-- Returns if product is from bespoke category
BEGIN
DECLARE @ret int;
SELECT @ret = SubCategory
FROM Inventory.Products
WHERE ProductId = @ProductID
IF (@ret >= 665) AND (@ret <= 668)
RETURN 1;
ELSE
RETURN 0;
RETURN NULL;
END;
如果我在 SQL 服务器中调用 PRINT [TBL].[fncIsBespoke](20334)
returns 1
但是当我在 VB.NET 中调用 GetIsBespoke(20334)
时 returns false?
将 SqlCommand 中的 SQL 修改为 select 函数的结果:
Dim obj_SqlCommand As New SqlCommand("SELECT [TBL].[fncIsBespoke]", obj_SqlConnection)
Further information
目前我在 SQL 服务器上有一个功能,它执行基本检查和 returns 1
(SQL 服务器数据类型 'bit') 如果为真,0
(SQL 服务器数据类型 'bit') 如果为假
这是我目前拥有的:
Public Shared Function GetIsBespoke(ByVal ProductId As Integer)
Dim Res As Boolean = False
Dim obj_SqlConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnectionString").ConnectionString)
Dim obj_SqlCommand As New SqlCommand("[TBL].[fncIsBespoke]", obj_SqlConnection)
obj_SqlCommand.CommandType = CommandType.StoredProcedure
Dim ProductId_SqlParameter As New SqlParameter("@ProductId", SqlDbType.Int)
ProductId_SqlParameter.Direction = ParameterDirection.Input
ProductId_SqlParameter.Value = ProductId
Dim Result_SqlParameter As New SqlParameter("@Result", SqlDbType.Bit)
Result_SqlParameter.Direction = ParameterDirection.ReturnValue
obj_SqlCommand.Parameters.Add(ProductId_SqlParameter)
obj_SqlCommand.Parameters.Add(Result_SqlParameter)
If Not IsDBNull(Result_SqlParameter.Value) Then
Res = Result_SqlParameter.Value
ElseIf IsDBNull(Result_SqlParameter.Value) Then
Res = False
End If
Return Res
End Function
USE [SRV]
GO
/****** Object: UserDefinedFunction [TBL].[fncIsBespoke] Script Date: 29/10/2019 2:46:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [TBL].[fncIsBespoke](@ProductID int)
RETURNS bit
AS
-- Returns if product is from bespoke category
BEGIN
DECLARE @ret int;
SELECT @ret = SubCategory
FROM Inventory.Products
WHERE ProductId = @ProductID
IF (@ret >= 665) AND (@ret <= 668)
RETURN 1;
ELSE
RETURN 0;
RETURN NULL;
END;
如果我在 SQL 服务器中调用 PRINT [TBL].[fncIsBespoke](20334)
returns 1
但是当我在 VB.NET 中调用 GetIsBespoke(20334)
时 returns false?
将 SqlCommand 中的 SQL 修改为 select 函数的结果:
Dim obj_SqlCommand As New SqlCommand("SELECT [TBL].[fncIsBespoke]", obj_SqlConnection)
Further information