3

我有一个简单的存储过程(从 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
4

1 回答 1

4

只需将这一行从

 Dim ds As DataSet = Nothing

 Dim ds = new DataSet()

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

 sa.Fill(Nothing) 

当然,Fill 代码并不认可这一点。

于 2013-07-13T12:12:43.617 回答