⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productshandler.vb

📁 asp.net技术内幕的书配源码
💻 VB
字号:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web

Public Class ProductsHandler
Implements IHttpHandler
  Public Sub ProcessRequest( objContext As HttpContext ) _
  Implements IHttpHandler.ProcessRequest
    Dim intProductID As Integer
    Dim conNorthwind As SqlConnection
    Dim strSelect As String
    Dim cmdSelect As SqlCommand
    Dim dtrProducts As SqlDataReader

    intProductID = GetProductID( objContext.Request.Path )
    conNorthwind = New SqlConnection( _
      "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
    strSelect = "Select ProductName, UnitPrice From Products" & _
      " Where ProductID=@ProductID"
    cmdSelect = New SqlCommand( strSelect, conNorthwind )
    cmdSelect.Parameters.Add( "@ProductID", intProductID )
    conNorthwind.Open()
    dtrProducts = cmdSelect.ExecuteReader( CommandBehavior.SingleRow )
    If dtrProducts.Read Then
      objContext.Response.Write( "<h2>Product Name:</h2>" )
      objContext.Response.Write( dtrProducts( "ProductName" ) )
      objContext.Response.Write( "<h2>Product Price:</h2>" )
      objContext.Response.Write( String.Format( _
        "{0:c}", _
        dtrProducts( "UnitPrice" ) ) )
    End If
    conNorthwind.Close()
  End Sub

  ReadOnly Property IsReusable() As Boolean _
  Implements IHttpHandler.IsReusable
    Get
      Return True
    End Get
  End Property

  Function GetProductID( strPath As String ) As Integer
    Dim intCounter As Integer
    Dim strNumbers As String

    For intCounter = 0 To strPath.Length - 1
      If Char.IsDigit( strPath.Chars( intCounter ) ) Then
        strNumbers &= strPath.Chars( intCounter )
      End If
    Next
    If Not strNumbers = Nothing Then
      Return cINT( strNumbers )
    Else
      Return -1
    End If
  End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -