Requin | Donc mauvais forum, voir dans programmation, voici mon code source que j'utilise pour me connecter à des DB Access en ASP 3.0... il faut lui passer une string de connexion valide avec le chemin de la DB et c'est à peu près tout. De préférence j'utilise le provider OLEDB.
Voici à quoi ressemble l'appel dans le code :
Code :
- Set objDB = New DBCONNClass
- Set objConn = objDB.Open(cfgDBAbsFilePath, cfgDBString, "", "", "" )
|
Voici le contenu des variables de l'appel ci-dessus (fichier de configuration) :
Code :
- ' database
- cfgDBPath = "../database/"
- cfgDBFile = "database.mdb"
- cfgDBRelFilePath = cfgDBPath & cfgDBFile
- cfgDBAbsFilePath = Server.Mappath(cfgDBRelFilePath)
- cfgDBString = "Provider=Microsoft.Jet.OLEDB.4.0;" &_
- "Data Source= " & cfgDBAbsFilePath & ";"
|
Voici la classe pour gérer mes connexions :
Code :
- <%
- ' /// Manual ///
- ' Pop()
- ' Desc : get current connection
- ' Pass : nothing
- ' Return : an ADODB.Connection object; If there is no connection set,
- ' return "Nothing"
- ' Open(l_strDBAbsFilePath, l_strDBConnString, l_strDBUser, _
- ' l_strDBPassword, l_strDBOptions)
- ' Desc : open a connection to a database using a DSN less method
- ' Pass : l_strDBAbsFilePath = a string, absolute file path to DB
- ' l_strDBConnString = a string, provider...
- ' l_strDBUser = a sring, user (may be an empty string)
- ' l_strDBPassword = a string, password (empty str)
- ' l_strDBOptions = a string, options (empty str)
- ' Return : current connection object, OR "Nothing"
- ' Close()
- ' Desc : close connection to database
- ' Pass : nothing
- ' Return : a boolean, True If transaction was sucessfull
- ' ExecuteRS(strStatement)
- ' Desc : execute a statement AND return a recordset
- ' Pass : strStatement = a string, SQL statement like SELECT
- ' Return : an ADODB.RecordSet object OR nothing If failed
- ' ExecuteNORS(strStatement)
- ' Desc : execute a statement AND don't return a recordset
- ' Pass : strStatement = a string, SQL statement like DELETE
- ' Return : True If there was no errors, False otherwise
- ' GetID()
- ' Desc : get an auto-incremented value from the database
- ' Pass : nothing
- ' Return : Unique ID... type set on the database
- Class DBCONNClass
- ' for storing object reference
- Private l_objConn
- '
- ' get current connection
- Public Function Pop()
- If IsObject(objStoreConn) Then
- Set Pop = l_objConn
- Else
- Set Pop = Nothing
- End If
- End Function
-
- '
- ' Connect to the database
- Public Function Open( _
- l_strDBAbsFilePath , _
- l_strDBConnString , _
- l_strDBUser , _
- l_strDBPassword , _
- l_strDBOptions _
- )
-
- ' local variables
- Dim l_strOpenString
- Dim l_strOpenUser
- Dim l_strOpenPassword
- Dim l_strOpenOptions
- Dim l_boolError
-
- l_boolError = False
-
- ' Instantiate ADO COM Object
- Set l_objConn = Server.Createobject("ADODB.Connection" )
-
- ' Check connection string parameter
- If (VarType(l_strDBConnString) <> vbError) AND _
- (l_strDBConnString <> "" ) Then
- ' Set connection string using parameter
- l_strOpenString = l_strDBConnString
- Else
- ' Error
- l_boolError = True
- End If
- ' Check user parameter
- If (VarType(l_strDBUser) <> vbError) Then
- ' Set user using parameter
- l_strOpenUser = l_strDBUser
- Else
- ' Error
- l_boolError = True
- End If
- ' Check password parameter
- If (VarType(l_strDBPassword) <> vbError) Then
- ' Set password using parameter
- l_strOpenPassword = l_strDBPassword
- Else
- ' Error
- l_boolError = True
- End If
- ' Check options parameter
- If (VarType(l_strDBUser) <> vbError) Then
- ' Set options using parameter
- l_strOpenOptions = l_strDBOptions
- Else
- ' Error
- l_boolError = True
- End If
- ' Open Connection to database
- If (l_strOptions <> "" ) AND _
- (l_strOpenUser <> "" ) AND _
- (l_strOpenPassword <> "" ) Then
-
- l_objConn.Open l_strOpenString , _
- l_strOpenUser , _
- l_strOpenPassword , _
- l_strOpenOptions
- ElseIf (l_strOpenUser <> "" ) AND _
- (l_strOpenPassword <> "" ) Then
-
- l_objConn.Open l_strOpenString , _
- l_strOpenUser , _
- l_strOpenPassword
- ElseIf (l_strOpenOptions <> "" ) Then
-
- l_objConn.Open l_strOpenString , _
- l_strOpenOptions
- Else
- l_objConn.Open l_strOpenString
- End If
- ' Begin an ADO MTS transaction
- l_objConn.BeginTrans
- ' check for errors
- If l_boolError Then
- Set Open = Nothing
- Else
- ' Returns Opened connection object
- Set Open = l_objConn
- End If
- End Function
- '
- ' Close connection to database
- Public Function Close()
- Dim l_boolError
- l_boolError = False
- ' Check for errors
- If l_objConn.Errors.Count = 0 Then
- ' Commit transaction If there are no errors
- l_objConn.CommitTrans
- Else
- ' Display errors
- Response.Write(l_objConn.Errors.Count & " error(s) : <br>" )
- For i = 0 To (l_objConn.Errors.Count-1)
- Response.Write("<a>Error " & i+1 & " )</a>" )
- Response.Write("<ul>" )
- Response.Write("<li>Description : " )
- Response.Write(l_objConn.Errors(i).Description)
- Response.Write("<li>HelpContext : " )
- Response.Write(l_objConn.Errors(i).HelpContext)
- Response.Write("<li>HelpFile : " )
- Response.Write(l_objConn.Errors(i).HelpFile)
- Response.Write("<li>NativeError : " )
- Response.Write(l_objConn.Errors(i).NativeError)
- Response.Write("<li>Number : " )
- Response.Write(l_objConn.Errors(i).Number)
- Response.Write("<li>Source : " )
- Response.Write(l_objConn.Errors(i).Source)
- Response.Write("<li>SQLState : " )
- Response.Write(l_objConn.Errors(i).SQLState)
- Response.Write("</ul>" )
- Next
- ' Rollback transaction If there are errors
- l_objConn.RollbackTrans
- l_boolError = True
- End If
- ' Free memory
- Set l_objConn = Nothing
- ' check for errors
- If l_boolError Then
- Close = False
- Else
- Close = True
- End If
- End Function
- '
- ' Execute using current connection AND return a recordset
- Public Function ExecuteRS(strStatement)
- If VarType(strStatement) <> vbError AND _
- strStatement <> "" AND _
- IsObject(l_objConn) Then
- ' execute adCmdText
- Set ExecuteRS = l_objConn.Execute(strStatement, ,1)
- Else
- Set ExecuteRS = Nothing
- End If
- End Function
- '
- ' Execute using current connection AND don't return a recordset
- Public Function ExecuteNORS(strStatement)
- If VarType(strStatement) <> vbError AND _
- strStatement <> "" AND _
- IsObject(l_objConn) Then
- ' execute adCmdText + adExecuteAsync
- l_objConn.Execute strStatement, , 129
- End If
- If l_objConn.Errors.Count = 0 Then
- ExecuteNORS = True
- Else
- ExecuteNORS = False
- End If
- End Function
- '
- ' Get unique ID of a freshly inserted recordset
- ' DO NOT WORK WITH REPLICATION ID (GUID)
- Public Function GetID()
- l_strSQL = "SELECT @@Identity"
- Set l_objRS = Server.CreateObject("ADODB.Recordset" )
- l_objRS.Open l_strSQL, l_objConn, 0, 1, 1 'forward only, readonly, cmd
- If Not(l_objRS.BOF AND l_objRS.EOF) Then
- varID = l_objRS(0).Value
- Else
- varID = ""
- End If
- l_objRS.Close
- If varID <> "" Then
- GetID = varID
- Else
- GetID = False
- End If
- End Function
- End Class
- %>
|
Message édité par Requin le 09-10-2003 à 14:56:54
|