将结果集从SQL Server存储过程返回到vb.net

时间:2015-06-20 10:59:41

标签: sql-server vb.net stored-procedures

以下存储过程在Visual Studio设计器中按我的意愿工作。结果是一个表格,其中包含输入@CourseName

的所有比赛距离
ALTER PROCEDURE [dbo].[getCourseDistancesProc]
    @CourseName nvarchar(50)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT DISTINCT
        RaceDistances.RaceDistance
    FROM  
        RacingMaster
    JOIN
        RaceDistances ON RacingMaster.Dist_Of_Race_FK = RaceDistances.PKRaceDistancesId
    JOIN
        Courses ON RacingMaster.RM_Course_FK = Courses.PKCourseId
    WHERE
        CourseName = @CourseName
END

我想从vb.net应用程序调用存储过程。我将哪种数据类型声明为输出变量,以便将完整的结果集返回给调用应用程序?

1 个答案:

答案 0 :(得分:0)

显然还有比我意识到的更多的工作要做,但是为了防止其他人遇到这个问题,我最终从其他地方改编的解决方案是: -

Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim results As String
Dim ConnectionString As String

' Create the connection string.
ConnectionString = "Data Source=*********;" & _
                    "Initial Catalog=*******;" & _
                    "Integrated Security=SSPI;"
myConn = New SqlConnection(ConnectionString)

myConn.Open()
Dim InputName As String
InputName = TextBox1.Text

myCmd = New SqlCommand()
myCmd.CommandText = "getCourseDistancesProc"
myCmd.CommandType = CommandType.StoredProcedure
myCmd.Parameters.AddWithValue("@CourseName", Odbc.OdbcType.NVarChar).Value = InputName
myCmd.Connection = myConn
Dim myReader As SqlDataReader = myCmd.ExecuteReader()

If myReader.HasRows Then

    Do While myReader.Read()
        Dim var As String
        var = myReader.GetString(0)
        MsgBox(var)
    Loop
Else
        MsgBox("No rows found.")
End If

myReader.Close()

显然,以上只是为了证明所请求的数据确实是从数据库中返回的。但现在我知道我可以用更有用的方式处理它。