Tuesday, August 01, 2006

ASP and Firebird

This is really a copy of a PDF file I made available on my old website, I'm posting it here because I'm trying to clean up things.
It's about Firebird and ASP pages, the most interesting thing is that it shows how to use transactions within ASP pages, which is not so common.

Connect to Firebird and retrieve data in ASP:

  1. ...
  2. on error resume next
  3. dim adoConn
  4. dim adoRS
  5. dim counter
  6. set adoConn = Server.CreateObject("ADODB.Connection")
  7. set adoRS = Server.CreateObject("ADODB.Recordset")
  8. adoConn.ConnectionString = "DRIVER={Firebird/Interbase(r) Driver};DBNAME=localhost:C:\Programmi\Firebird\Firebird_1_5\examples\EMPLOYEE.FDB;UID=SYSDBA;PWD=masterkey"
  9. adoConn.Open
  10. adoRS.ActiveConnection = adoConn
  11. if adoConn.errors.count = 0 then
  12. response.write "Fields In The 'Contacts' Table:"
  13. adoRS.Open "select e.first_name, e.last_name, d.department, p.proj_name from employee e right outer join
  14. department d on e.dept_no = d.dept_no left outer join employee_project ep on e.emp_no = ep.emp_no left outer join project p on
  15. ep.proj_id = p.proj_id", adoConn
  16. nfields = adoRS.fields.count
  17. while not adoRS.EOF
  18. for i = 0 to nfields -1
  19. response.write adoRS.fields(i).name & ": " & adoRS.fields(i).value
  20. & " "
  21. next
  22. response.write "
    "
  23. adoRS.MoveNext
  24. wend
  25. else
  26. response.write "ERROR: Couldn't connect to database"
  27. end if
  28. adoRS.Close
  29. if adoConn.errors.count = 0 then
  30. response.write "

    Second example:

    "
  31. adoRS.Open "select count(e.emp_no) as emp_4_proj, d.department, p.proj_name from employee e right outer join
  32. department d on e.dept_no = d.dept_no left outer join employee_project ep on e.emp_no = ep.emp_no left outer join project p on
  33. ep.proj_id = p.proj_id group by d.department, p.proj_name", adoConn
  34. nfields = adoRS.fields.count
  35. while not adoRS.EOF
  36. for i = 0 to nfields -1
  37. response.write adoRS.fields(i).name & ": " & adoRS.fields(i).value
  38. & " "
  39. next
  40. response.write "
    "
  41. adoRS.MoveNext
  42. wend
  43. else
  44. response.write "ERROR: Couldn't connect to database"
  45. end if
  46. adoConn.Close
  47. Set adoRS = nothing
  48. Set adoConn = nothing
  49. ...


Using transactions within ASP pages:

  1. if adoConn.errors.count = 0 then
  2. 'here we begin a transaction
  3. adoConn.begintrans
  4. 'now we add a row to the “country” table
  5. adoConn.execute "insert into country values ('Portugal', 'Escudo')"
  6. 'we commit the transaction, thus saving changes
  7. adoConn.CommitTrans
  8. 'now we start another transaction
  9. adoConn.begintrans
  10. 'here we delete the newly added row
  11. adoconn.execute "delete from country where country = 'Portugal'"
  12. 'this time we rollback the transaction, not saving the changes
  13. adoconn.rollbacktrans
  14. 'this query will show that the commit and rollback sequence has been executed and the row with ‘Portugal’ and ‘Escudo’ is present
  15. adoRS.Open "select * from country", adoConn

Note that your decision on transaction usage and the commit/rollback sequence must be business driven, no guesswork at all!!
The original paper is available here (PDF)

No comments: