提问人:Ann 提问时间:7/30/2020 最后编辑:GSergAnn 更新时间:7/30/2020 访问量:237
Vb.net 函数不返回 SQL Server 存储过程结果
Vb.net function not returning the SQL Server stored procedure result
问:
我有一个存储过程,它根据传入的参数从表中获取数据:
SELECT *
FROM [dbo].Table1
WHERE Column1 = @Param1 AND Column2 = @Param2
现在,我使用以下代码通过 VB.Net 函数调用 SQL Server 存储过程:
Public Function GetSPValues(ByVal Param1 As Integer,
ByVal Param2 As String) As DataTable
Dim sqlCon As SqlConnection
Dim dt As DataTable = New DataTable()
sqlCon = strCon
Using (sqlCon)
Dim cmd As New SqlCommand
cmd.Connection = sqlCon
cmd.CommandText = "GetSP"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Param1", Param1)
cmd.Parameters.AddWithValue("@Param2", Param2)
sqlCon.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
dt.TableName = "SPValuesTable"
dt.Load(reader)
Return dt
End Using
End Function
问题是,当我尝试在 SQL Server Management Studio 中执行存储过程时,它工作正常并返回我需要的所有数据。
但是当我尝试从 VB.NET 调用时,它根本没有返回任何内容。
我不确定我传递的参数是否真的是存储过程。你能帮我吗?
更新:这是我的整个SP:
ALTER PROCEDURE [dbo].[GetSP]
@Param1 int = null,
@Param2 nvarchar(50) = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * FROM dbo.tbl1 WHERE Column1 = @Param1 AND Column2
= @Param2
END
答: 暂无答案
评论
.AddWithValue()
sqlCon
sqlCon = strCon
Options -> Project and Solutions -> VB default
Option Strict On