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

📄 global.asax

📁 ASP.NET Web Forms Techniques
💻 ASAX
字号:
<%@Application Language="VB" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<%@Import Namespace="System.Diagnostics" %>
<%@Import Namespace="System.IO" %>

<script runat="server">
'------------------------------------------------------
Dim sLogName As String = "XroxCarCompany"
Dim sLogFile As String = "C:\Temp\XroxCarCoLog.txt"

Sub Application_Start(Sender As Object, E As EventArgs)
  ' code that runs on application startup
  ' update database table containing calculated PMT values
  ' for calculating vehicle finance payments

  Dim sInterestRate, sStoredProc, sConnect, sMessage As String
  Dim fInterestRate, fPMTResult As Double
  Dim fPaymentPer1000 As Decimal
  Dim iMonth As Integer

  ' get values from web.config.file
  sInterestRate = ConfigurationSettings.AppSettings("XroxCarsInterestRate")
  sConnect = ConfigurationSettings.AppSettings("XroxCarsConnectString")

  ' declare stored proc name and data access objects
  sStoredProc = "UpdatePmtFinanceData"
  Dim sqlConn As New SqlConnection(sConnect)
  Dim sqlComm As New SqlCommand(sStoredProc, sqlConn)
  sqlComm.CommandType = CommandType.StoredProcedure

  Try

    ' parse interest rate into a Double
    fInterestRate = Double.Parse(sInterestRate)

    ' open database connection
    sqlConn.Open()

    ' calculate payment rate per $1000 at six-month intervals
    For iMonth = 6 To 120 Step 6

      fPMTResult = - Pmt(fInterestRate / 1200, iMonth, 1000, 0)
      fPaymentPer1000 = Decimal.Round(CType(fPMTResult, Decimal), 2)

      ' set parameters and call stored proc to update database table row
      sqlComm.Parameters.Clear()
      sqlComm.Parameters.Add("@Months", iMonth)
      sqlComm.Parameters.Add("@Payment", fPaymentPer1000)
      sqlComm.ExecuteNonQuery()

    Next

    ' write a "succeeded" message to error log
    sMessage = "Updated FinancePMTData values in database"
    WriteEventLogMessage(sLogName, sMessage, EventLogEntryType.Information)
    WriteErrorFileMessage(sLogFile, sMessage)

  Catch

    ' write a "failed" message to error log
    sMessage = "Unable to update FinancePMTData values in database"
    WriteEventLogMessage(sLogName, sMessage, EventLogEntryType.Error)
    WriteErrorFileMessage(sLogFile, sMessage)

  Finally
    sqlConn.Close()    ' close the database connection
  End Try

End Sub

'------------------------------------------------------

Sub Application_End(Sender As Object, E As EventArgs)
  ' code that runs on application shutdown
End Sub

'------------------------------------------------------

Sub Application_Error(Sender As Object, E As EventArgs)
  ' code that runs when an unhandled error occurs

  Dim sMessage As String = Server.GetLastError().ToString()
  WriteEventLogMessage(sLogName, sMessage, EventLogEntryType.Error)
  WriteErrorFileMessage(sLogFile, sMessage)

End Sub

'------------------------------------------------------

Sub Session_Start(Sender As Object, E As EventArgs)
  ' code that runs when a new session is started
End Sub

'------------------------------------------------------

Sub Session_End(Sender As Object, E As EventArgs)
  ' code that runs when a session ends
End Sub

'------------------------------------------------------

Sub WriteEventLogMessage(sLogName As String, _
                         sMessage As String, _
                         eType As EventLogEntryType)

  Try
    ' write a string message to the system's Event Log
    ' create the event log if it does not already exist
    ' however the ASPNET account must have relevant
    ' permissions for this to work - it does not by default
    If (Not EventLog.SourceExists(sLogName)) Then
      EventLog.CreateEventSource(sLogName, sLogName)
    End if

    ' write event to log
    Dim oLog as New EventLog
    oLog.Source = sLogName
    oLog.WriteEntry(sMessage, eType)
  Catch
  End Try

End Sub

'------------------------------------------------------

Sub WriteErrorFileMessage(sFilePath As String, _
                          sMessage As String)

  ' write a string to the text log file sFilePath
  ' creates the file if it does not already exist
  Dim oWriter As StreamWriter

  Try
    oWriter = New StreamWriter(sFilePath, True)
    oWriter.WriteLine(Now().ToString & " - " & sMessage)
  Catch
  Finally
    oWriter.Close()
  End Try

End Sub

'------------------------------------------------------
</script>

⌨️ 快捷键说明

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