Hello!
Jag har gjort en funktion som jag skulle vilja dela med mig av.
Jag sitter just nu som bäst och läser igen Professional ASP 3.0 (BRA bok, köp den ifall ni har lite tidigare erfarenhet av ASP). Och nu e jag på ADO 2.5, men eftersom inte alla webbservrar har ver 2.5 utav ADO, så tänkte jag att jag gör en funktion som kolla vilken ver av ADO som finns installerad. Och samtidigt använder mig av all Error Handling och annat sådant jag lärt mig bättre nu.
Det blev lite mer avancerat än vad jag hade tänkt mig. Det blev kanon seriös till slut. Men nu så kanske jag gör så att jag lägger in lite saker som kollar ifall filesystemobject och global.asa funkar och lite sådant (finns redan en applikation som kollar vilka objekt som finns installerade, jag vet. Men jag tänkte samla det hela). Det är ju inte alla webservrar som har det aktiverat (och speciellt inte alla gratis servrar). Så därför tänkte jag att det kunde vara bra ifall man hade ett script som kollade allt sådant (om jag nu Orkar göra det. Det finns väl antagligen redan något som kollar sådant antar jag).
Nu vill jag att ni ska kolla på scriptet och säga vad ni tycker.
Det finns för nerladdning Här or ett exempel som visar hur det ser ut Här.
Koden (med risk att du får scrolla ;)):
<%Option Explicit
Response.Buffer = true
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
This page is made and copyrighted (c) 2000 by Magnus Andersson
All rights reserved.
Contact Magnus Andersson at: zappod@hem.passagen.se
-->
<html>
<head>
<title>ADO Version check</title>
<style type="text/css">
/*Body Propeties*/
A {text-decoration:none;color:black;}
A:Visited {text-decoration:none;color:black;}
A:Active {text-decoration:none;color:black;}
A:Hover {text-decoration:none;color:black;}
BODY {font-family:Verdana,arial;font-size:8pt;color:black;}
.Text {font-family:Verdana,arial;font-size:8pt;color:black;}
</style>
<%
'*******************************************************************
'Name: ADO Version Check
'Purpose: for reciving ADO version number and properties
'Inputs: None
'Returns: ADO Version number and some ADO properties
'Autor: Magnus Andersson
'Mail: zappod@hem.passagen.se
'URL: [url="http://www.designmodule.com.bi"]http://www.designmodule.com.bi[/url]
'Note: This is freeware as long as this message remains intact.
'I will not under any circumstanses give any support for this script.
'And I am not responsible for any dataloss and/or any damage this
'may give you, your hardware, friends or family. ;)
'********************************************************************
Function fncCheckADOVersion()
Dim objCn, strCn, intConnType, strSQLDatSrc, strSQLUID, strSQLPWD, strSQLDB, strAcDatSrc, _
strAcPWD, strResponse, strVersionInfo, objError, strProp
strProp = false
'This is the section where you insert your values.
'----------------------------------------------------------------------------------------------
'Select type of connection
'Connection type:
'intConnType = 1 for ODBC driven SQL Connection
'intConnType = 2 for ODBC driven Access Connection
'intConnType = 3 for OLE DB driven SQL Connection
'intConnType = 4 for OLE DB driven Access Connection
intConnType = 4
'Settings for SQL Connection (if you've choosen either 1 or 3 as connection type)
strSQLDatSrc = "" 'SQL database Source, i.e Computername or IP Adress
strSQLUID = "" 'SQL database Username
strSQLPWD = "" 'SLQ database Password
strSQLDB = "" 'SQL database initial catalog
'Settings for Access Connection if you've choosen either 2 or 4 as connection type)
strAcDatSrc = "" 'Access Database Source, the path to your *.mdb, a relative path
strAcPWD = "" 'Access Database Password, Leave blank if you don't have any
'Do not edit anything beyond this point
'----------------------------------------------------------------------------------------------
SELECT CASE intConnType
CASE 1 :
'ODBC SQL Connection (Use ODBC to rechive all the properties)
strCn = "driver={SQL Server};server="&strSQLDatSrc&"; user id="&strSQLUID&"; Password="&strSQLPWD&"; Database="&strSQLDB&";"
CASE 2 :
'ODBC Acess Connection (Use ODBC to rechive all the properties)
strCn="Driver={Microsoft Access Driver (*.mdb)}; DBQ="&Server.Mappath(strAcDatSrc)&"; uid=; pwd="&strAcPWD&";"
CASE 3 :
'OLE DB SQL Connection (Remove/Comment properties specific for ODBC)
strCn="Provider=SQLOLEDB; Data Source="&strSQLDatSrc&"; Initial Catalog="&strSQLDB&"; User Id="&strSQLUID&"; Password="&strSQLPWD&";"
CASE 4 :
'OLE DB Acess Connection (Remove/Comment properties specific for ODBC
strCn="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.Mappath(strAcDatSrc)
strProp = true
CASE ELSE :
Response.write "You must specify a connection type.<br><br><b>These are the avialible values:</b> <br>"&_
"<b>intConnType = 1</b> for ODBC driven SQL Connection <br>" &_
"<b>intConnType = 2</b> for ODBC driven Access Connection <br>" &_
"<b>intConnType = 3</b> for OLE DB driven SQL Connection <br>" &_
"<b>intConnType = 4</b> for OLE DB driven Access Connection"
Response.Flush
Response.End
END SELECT
'response.write strCn & "<br>"
'Open connection.
On Error Resume Next
Set objCn = Server.CreateObject("ADODB.Connection")
If IsObject(objCn) then
If len(strProp) Then
objCn.Properties("JET OLEDB:Database Password") = strAcPWD
End If
objCn.Open strCn
'Connection Error checking.
'Examine if there is any errors in the Connection Object
If objCn.Errors.Count > 0 Then
For Each objError in objCn.Errors
'Error with number 0 are informational
If objError.Number <> 0 Then
Response.write "There was a problem when connecting to the database, please check you connection setting<br><br>" &_
"<b>Your connection values are as follows:</b> <br><br>"
If (intConnType = 1) or (intConnType = 3) Then
response.write "strSQLDatSrc = <b>" & strSQLDatSrc & "</b> (SQL database Source, i.e Computername or IP Adress)<br>" &_
"strSQLUID = <b>" & strSQLUID & "</b> (SQL database Username)<br>" &_
"strSQLPWD = <b>" & strSQLPWD & "</b> (SLQ database Password)<br>" &_
"strSQLDB = <b>" & strSQLDB & "</b> (SQL database initial catalog)<br><br>" &_
"Your whole connection string looks like this: <br>" &_
"<b>"&strCn&"</b>"
Else
If strAcPWD = "" Then strAcPWD = "Blank"
response.write "strAcDatSrc = <b>" & strAcDatSrc & "</b> (Access Database Source, the path to your *.mdb, a <b>relative</b> path)<br>" &_
"strAcPWD = <b>" & strAcPWD & "</b> (Access Database Password, Leave blank if you don't have any)<br><br>"&_
"Your whole connection string looks like this: <br>" &_
"<b>"&strCn&"</b>"
End If
Response.Flush
Response.End
End If
Next
End If
On Error Goto 0
'General Properties
If intConnType <> 4 Then
strVersionInfo = "<TR><TD>ADO Version:</TD><TD><b>" & objCn.Version & "</b></TD></TR>" & chr(13) &_
"<TR><TD>DBMS Name:</TD><TD><b>" & objCn.Properties("DBMS Name") & "</b></TD></TR>" & chr(13) &_
"<TR><TD>DBMS Version:</TD><TD><b>" & objCn.Properties("DBMS Version") & "</b></TD></TR>" & chr(13) &_
"<TR><TD>OLE DB Version:</TD><TD><b>" & objCn.Properties("OLE DB Version") & "</b></TD></TR>" & chr(13) &_
"<TR><TD>Provider Name:</TD><TD><b>" & objCn.Properties("Provider Name") & "</b></TD></TR>" & chr(13) &_
"<TR><TD>Provider Version:</TD><TD><b>" & objCn.Properties("Provider Version") & "</b></TD></TR>" & chr(13)
Else
strVersionInfo = "<TR><TD>ADO Version:</TD><TD><b>" & objCn.Version & "</b></TD></TR>" & chr(13) &_
"<TR><TD>Info:</TD><TD><b>Don't ask me why, but JET doesn't seem to have the same properties as the other connection types.</b></TD></TR>" & chr(13)
End If
If (intConnType = 1) or (intConnType = 2) Then
'Properties specific for ODBC connection.
strVersionInfo = strVersionInfo & _
"<TR><TD>Driver Name:</TD><TD><b>" & objCn.Properties("Driver Name") & "</b></TD></TR>" & chr(13) &_
"<TR><TD>Driver Version:</TD><TD><b>" & objCn.Properties("Driver Version") & "</b></TD></TR>" & chr(13) &_
"<TR><TD>Driver ODBC Version:</TD><TD><b>" & objCn.Properties("Driver ODBC Version") & "</b></TD></TR>"& chr(13)
End If
'Put everything in a variable for the response of the function
strResponse = chr(13) & "<b>ADO Properties:</b><br>" &_
chr(13) & "<TABLE class='text' border='1' cellpadding='4' cellspacing='1' width='60%'><TR>"&chr(13) &_
"<TD bgcolor='#C0C0C0' align='Left' valign='top'><STRONG>Property</STRONG></TD>"&chr(13) &_
"<TD bgcolor='#C0C0C0' align='Left' valign='top'><STRONG>Value</STRONG></TD></TR>" & chr(13) &_
strVersionInfo &_
"<TR></tr><TD bgcolor='#C0C0C0' colspan='2' align='Left' valign='top'><STRONG>With connection string: </STRONG>"&strCn&"</TD></TR></TABLE>"
'Close down the connection object and make it avalible for OLE DB connection pooling
If intConnType <> 4 Then ObjCn.Close
Set ObjCn = Nothing
fncCheckADOVersion = strResponse
Else
Response.write "Could not initiate an instance of the ADODB.Connection Object"
Response.Flus
Response.End
End If
End Function
%>
</head>
<body marginheight="10" marginwidth="10" topmargin="10" leftmargin="10" bgcolor="white">
<!-- Output -->
<% = fncCheckADOVersion() %>
</body>
</html>
------------------
/Magnus aka Zaphod
www.designmodule.com.bi
[Redigerat av Zaphod den 11 jul 2000]
[Redigerat av Zaphod den 12 jul 2000]