SqlDataReader 收集一行值而不是所有行?对 SqlDataReaders 感到困惑和陌生
SqlDataReader collecting one row value instead of all rows? Confused & new to SqlDataReaders
两个表:
CART(CartID、UserID、DateCreated、ProductID、ProductName、Size、Price、Quantity、Subtotal、HoursWork)
产品(产品 ID、名称、描述、价格、尺寸、图片、缩略图、重量、描述、库存、类别 ID、工时)
我正在尝试做的事情:
根据 SELECT 语句选择的 all 类别 ID 显示两个按钮之一。
类别 ID 范围为 1-7。如果选择的任何类别 ID 匹配“1”、“2”或“7”,则应显示 'Create Booking' 按钮,否则应显示 'Delivery Option' 按钮。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Display the correct button:
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = "SELECT products.CategoryID FROM Products INNER JOIN Cart on products.ProductID = cart.ProductID WHERE ([UserID] = @UserID) AND ([DateCreated] = CONVERT(date, GETDATE()))"
Dim UserID As SqlParameter = New SqlParameter("@UserID", SqlDbType.NVarChar, 128)
UserID.Value = "249f38b8-e745-44f4-8d78-806b0e5e7e0e"
cmd.Parameters.Add(UserID)
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
Dim reader As SqlDataReader = cmd.ExecuteReader
Dim CategoryID As Integer
While reader.Read()
'CategoryID = reader.GetValue(0)
CategoryID = CType(reader("CategoryID"), Integer)
End While
conn.Close()
lblTest.Text = CategoryID
If CategoryID = "1" Or "2" Or "7" Then
btnCreateBooking.Visible = True
Else
btnDeliveryOptions.Visible = True
End If
End Sub
然而,购物车中last产品的CategoryID被SqlDataReader生成到label/variable,按钮'Create Booking'是只有连显示。
在这里找到答案:https://forums.asp.net/t/1401666.aspx?C+datareader+returning+multiple+rows+and+asp+Label+question+
If reader.HasRows Then
While reader.Read()
lblTest.Text = (lblTest.Text _
+ (reader("CategoryID").ToString + "<br>"))
End While
conn.Close()
End If
并将我的 if 语句更正为:
If lblTest.Text.Contains("1") Or lblTest.Text.Contains("2") Or lblTest.Text.Contains("7") Then
btnCreateBooking.Visible = True
Else
btnDeliveryOptions.Visible = True
End If
两个表:
CART(CartID、UserID、DateCreated、ProductID、ProductName、Size、Price、Quantity、Subtotal、HoursWork)
产品(产品 ID、名称、描述、价格、尺寸、图片、缩略图、重量、描述、库存、类别 ID、工时)
我正在尝试做的事情: 根据 SELECT 语句选择的 all 类别 ID 显示两个按钮之一。
类别 ID 范围为 1-7。如果选择的任何类别 ID 匹配“1”、“2”或“7”,则应显示 'Create Booking' 按钮,否则应显示 'Delivery Option' 按钮。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Display the correct button:
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = "SELECT products.CategoryID FROM Products INNER JOIN Cart on products.ProductID = cart.ProductID WHERE ([UserID] = @UserID) AND ([DateCreated] = CONVERT(date, GETDATE()))"
Dim UserID As SqlParameter = New SqlParameter("@UserID", SqlDbType.NVarChar, 128)
UserID.Value = "249f38b8-e745-44f4-8d78-806b0e5e7e0e"
cmd.Parameters.Add(UserID)
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
Dim reader As SqlDataReader = cmd.ExecuteReader
Dim CategoryID As Integer
While reader.Read()
'CategoryID = reader.GetValue(0)
CategoryID = CType(reader("CategoryID"), Integer)
End While
conn.Close()
lblTest.Text = CategoryID
If CategoryID = "1" Or "2" Or "7" Then
btnCreateBooking.Visible = True
Else
btnDeliveryOptions.Visible = True
End If
End Sub
然而,购物车中last产品的CategoryID被SqlDataReader生成到label/variable,按钮'Create Booking'是只有连显示。
在这里找到答案:https://forums.asp.net/t/1401666.aspx?C+datareader+returning+multiple+rows+and+asp+Label+question+
If reader.HasRows Then
While reader.Read()
lblTest.Text = (lblTest.Text _
+ (reader("CategoryID").ToString + "<br>"))
End While
conn.Close()
End If
并将我的 if 语句更正为:
If lblTest.Text.Contains("1") Or lblTest.Text.Contains("2") Or lblTest.Text.Contains("7") Then
btnCreateBooking.Visible = True
Else
btnDeliveryOptions.Visible = True
End If