从aspx中的数据库中获取数据

时间:2010-07-27 12:45:44

标签: asp.net database visual-studio-2010

我正在使用VS 2010来开发一个从数据库中获取一些数据的站点。

但我只是不断收到此错误。

'没有给出一个或多个必需参数的值。'

这是我的代码

Public Function getSessionDetails(ByVal roomId As String) As ArrayList

Dim sql As String
    sql = "SELECT * FROM Table1 WHERE RoomId = ?"
    Dim dbComm As New OleDbCommand(sql, dbConn)

    'dbComm.Connection.Open()
    dbComm.CommandText = sql
    dbComm.Parameters.Add("RoomId", System.Data.OleDb.OleDbType.BSTR).Value = roomId
    dbConn.Open()
    'dbComm.ExecuteNonQuery()


    Dim dbRead As OleDbDataReader = dbComm.ExecuteReader() 'System.Data.CommandBehavior.CloseConnection)

    Dim arr As New ArrayList

    Try
        While dbRead.Read()
            arr.Add(New SessionDetails(dbRead.GetValue(2).ToString, dbRead.GetValue(3).ToString, dbRead.GetValue(4).ToString, dbRead.GetValue(6).ToString, _
                                       dbRead.GetValue(5).ToString, dbRead.GetValue(7).ToString, dbRead.GetValue(8).ToString))
        End While
    Catch ex As Exception

    End Try

    Try
        'sort arr by date using bubble
        For i As Integer = 1 To arr.Count - 1
            Dim sesTop As SessionDetails = arr(i)
            Dim sesBot As SessionDetails = arr(i - 1)
            Dim sesTmp As SessionDetails

            If sesBot.sessionDate < sesTop.sessionDate Then
                'swap them
                sesTmp = arr(i)
                arr(i) = arr(i - 1)
                arr(i - 1) = sesTmp
                i = 0
            ElseIf sesBot.sessionDate = sesTop.sessionDate And sesBot.StartTime < sesTop.StartTime Then
                'swap them
                sesTmp = arr(i)
                arr(i) = arr(i - 1)
                arr(i - 1) = sesTmp
                i = 0
            End If
        Next
    Catch ex As Exception

    End Try

    'Dim se As SessionDetails
    'For Each se In arr
    '    If se.sessionDate < Now Then
    '        arr.Remove(se)
    '    End If
    'Next

    dbRead.Close()
    dbConn.Close()

    Return arr
End Function

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

更改这两行

sql = "SELECT * FROM Table1 WHERE RoomId = @RoomId"


dbComm.Parameters.Add("@RoomId", System.Data.OleDb.OleDbType.BSTR).Value = roomId

答案 1 :(得分:0)

sql =“SELECT * FROM Table1 WHERE RoomId =? - 这是错误的查询。

正确的查询是'sql =“SELECT * FROM Table1 WHERE RoomId = @RoomId'

dbComm.Parameters.Add(“@ RoomId”,System.Data.OleDb.OleDbType.BSTR).Value = RoomId