如何运行返回数据集的存储过程

时间:2013-07-13 12:11:33

标签: sql-server vb.net

我有一个简单的存储过程(选择名称,来自MyTable的ID),我想从C#(或VB.NET)调用它来填充数据集。

这是我的代码:

Public Class PaymentDataAccess

    Public Function GetPaymentData() As DataSet

        Dim cn As New SqlClient.SqlConnection
        cn.ConnectionString = "Data Source=WORK-HP\BTFSERVER1;Initial Catalog=PaymentReminder;Integrated Security=True"

        Dim Cmd As New SqlCommand("GetPaymentData", cn)

        Cmd.CommandType = CommandType.StoredProcedure

        Dim sa As New SqlDataAdapter(Cmd)

        cn.Open()

        Dim ds As DataSet = Nothing
        Try

            sa.Fill(ds)

        Catch ex As Exception
            Dim i As Integer = 7
        End Try
        Return ds

    End Function


End Class

我在sa.Fill(ds)

收到例外
{"Value cannot be null.
Parameter name: dataSet"}
    System.ArgumentNullException: {"Value cannot be null.
Parameter name: dataSet"}

这是我的存储过程:

USE [PaymentReminder]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetPaymentData] 

AS
BEGIN

    SET NOCOUNT ON;   
    SELECT * from Payments
END

1 个答案:

答案 0 :(得分:4)

只需从

更改此行
 Dim ds As DataSet = Nothing

 Dim ds = new DataSet()

您需要将初始化的DataSet传递给SqlDataAdapter.Fill方法 实际上你的代码就像

 sa.Fill(Nothing) 

当然,填写代码并不理解这一点。