Mar 9, 2010

Classic ASP Basics - 1

global.asa
==========

application events
session events
object declarations
TypeLibrary declarations
#include directive

======================




You could reference the object "MyAd" from any page in the ASP application:

<%=MyAd.GetAdvertisement("/banners/adrot.txt")%>

========================

Calling stored procedure from ADO
=================================

Set cn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
cn.Open "data source name", "userid", "password"
Set cmd.ActiveConnection = cn
cmd.CommandText = "sp_test"
cmd.CommandType = adCmdStoredProc
' Ask the server about the parameters for the stored proc
cmd.Parameters.Refresh
' Assign a value to the 2nd parameter.
' Index of 0 represents first parameter.
cmd.Parameters(1) = 11
cmd.Execute
%>
Calling via method 1

ReturnValue = <% Response.Write cmd.Parameters(0) %>

Cursors types in ADO
====================

1) adOpenDynamic :- Additions, Changes and Deletions by other users will be visible
2) adOpenKeyset :- Only changes will be visible. Additions and Deletions are not visible.
3) adOpenStatic :- Additions, Changes and Deletions by other users are not visible. Used for disconnected recordset model.
4) adOpenForward-only :- Additions, Changes and Deletions by other users are not visible. To only scroll forward only.

Cursor Locations in ADO
=======================

adUseNone
adUseServer
adUseClient (read only)
adUseClientBatch

Cursor Lock types in ADO
=======================

adLockPessimistic - Locks the row once after any edits occur.
adLockOptimistic - Locks the row only when Update is called.
adLockBatchOptimistic - Allows Batch Updates.
adLockReadOnly - Read only. Cannot alter the data.

Disconnected Recordset
=======================

set con= server.createobject("ADODB.Connection")
set rs= server.createobject("ADODB.Recordset")
rs.CursorLocation=adUseClient
con.open sqlcon
rs.open sqlqry, con, adOpenStatic, adLockBatchOptimistic
Set oRS.ActiveConnection = Nothing
Set GetanotherRS = oRS

Clustered and nonclustered indexes
=================================

Clustered indexes contains actual data at leaf nodes
non clustered indexes contains row locators that point to actual data. 249 -> 999


2 comments:

  1. sub Application_OnStart
    'some code
    end sub

    sub Application_OnEnd
    'some code
    end sub

    sub Session_OnStart
    'some code
    end sub

    sub Session_OnEnd
    'some code
    end sub




    object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator"
    object

    Type Library: used to store information about COM. Used to get constants defined in COM from ASP application

    ReplyDelete
  2. Saving and retrieving XML into Recordset in VB
    =============================================

    Public Function SaveRSToXML(ByVal ConnectionString As String, _
    ByVal SQLString As String, ByVal FullPath As String) As Boolean
    '**************************************************
    'PURPOSE: SAVE A RECORDSET TO AN XML FILE USING
    'ADO 2.5

    'PARAMETERS:
    'ConnectionString: Valid Connection String
    'SQLString: Valid SQL Statement for Data Source specified
    ' in ConnectionString
    'FullPath: FullPath of XMLFile to write to

    'RETURNS: True if Sucessful, false otherwise
    'REQUIRES: Installation of and reference to ADO 2.5
    'EXAMPLE of SaveRsToXML and LoadRSToXML:

    'Dim sConnString As String
    'Dim sSQL As String
    'Dim oRs As ADODB.Recordset
    'Dim iCtr As Integer

    'sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
    'Source=C:\MyDb.mdb"
    'sSQL = "select * from MyTable"
    'SaveRSToXML sConnString, sSQL, "C:\My Documents\MyRs.xml"

    'Set oRs = LoadRsFromXML("C:\my documents\MyRS.xml")
    'If Not oRs Is Nothing Then
    ' Do While Not oRs.EOF
    ' For iCtr = 0 To oRs.Fields.Count - 1
    ' Debug.Print oRs.Fields(iCtr).Name & " = " _
    ' & oRs.Fields(iCtr).Value & ";";
    ' Next
    ' Debug.Print vbCrLf
    ' oRs.MoveNext
    ' Loop
    ' set oRs = nothing
    'End If


    '******************************************************

    Dim oCn As New ADODB.Connection
    Dim oCmd As New ADODB.Command
    Dim oRs As New ADODB.Recordset

    On Error GoTo ErrorHandler

    oCn.ConnectionString = ConnectionString
    oCn.Open()
    oCmd.ActiveConnection = oCn
    oCmd.CommandText = SQLString
    oCmd.CommandType = adCmdText
    oRs = oCmd.Execute
    oRs.Save(FullPath, adPersistXML)
    SaveRSToXML = True

    ErrorHandler:
    On Error Resume Next
    oRs = Nothing
    oCmd = Nothing
    If oCn.State <> 0 Then oCn.Close()
    oCn = Nothing

    End Function

    Public Function LoadRsFromXML(ByVal FullPath As String) As _
    ADODB.Recordset

    '**************************************************
    'PURPOSE: LOAD A RECORDSET FROM AN XML FILE USING
    'ADO 2.5. THE XML FILE MUST HAVE BEEN SAVED
    'USING SAVE METHOD OF RECORDSET OBJECT WITH adPersistXML AD
    'SECOND PARAMETER

    'PARAMETERS:
    'FullPath: FullPath of XMLFile to load

    'RETURNS: Reference to a Recordset Object, or Nothing if
    ' Function fails
    'REQUIRES: Installation of and reference to ADO 2.5
    'EXAMPLE: See Example for SaveRsToXML

    '******************************************************

    Dim oRs As New ADODB.Recordset
    On Error Resume Next

    If Dir(FullPath) = "" Then Exit Function
    oRs.Open(FullPath, "Provider=MSPersist;", adOpenForwardOnly, _
    adLockReadOnly, adCmdFile)

    If Err.Number = 0 Then
    LoadRsFromXML = oRs
    End If

    End Function

    ReplyDelete