1

I am building a school project which is a room booking service. I have reached the part of the programming where I need to figure out how to store bookings. I know how to pull from a database using VB and I've laid it out like that when I try to pull booking information from the database.

    strBookingQuery = "SELECT * FROM bookings WHERE Date = '" & InputBookingDate & "'"
    Dim Cmd As New MySqlCommand(strBookingQuery, Newconnection)
    Newconnection.ConnectionString = strServerString
    Newconnection.Open()
    reader = Cmd.ExecuteReader()
    reader.Read()

    StartPeriod = reader.GetInt16(1)
    Length = reader.GetInt16(2)
    UserID = reader.GetInt16(3)
    RoomID = reader.GetInt16(4)
    Newconnection.Close()

But when it gets to StartPeriod = reader.GetInt16(1) it says Invalid attempt to access a field before calling Read(), is this because MySQL stores dates in a different way? Or something else? Any help would be greatly appreciated :)

4

1 回答 1

1

您希望GetInt16如何读取date?

Reader.GetInt16() 以 16 位有符号整数形式获取指定列的值。不执行任何转换;因此,检索到的数据必须已经是 Int16 或可强制转换为 Int16。

鉴于 OP 确认 startPeriod 没有问题,可能的问题可能是:

在调用 read() 之前访问字段的尝试无效

于 2013-01-29T14:19:08.247 回答