Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”

Similar presentations


Presentation on theme: "ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”"— Presentation transcript:

1 ASP & ADO

2 Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales” Opening multiple recordsets without connection object create multiple connections Multiple Recordsets on One Connection Object (p192). –Demo:RecSetConn.ASP

3 Creating Recordset Using Recordset Object RecordsetName.Open Source, ActiveConnection, CursorType, LockType, Options Source: Command object name, an SQL statement, a table name, a stored procedure. ActiveConnection: Connection string or DSN. Needed if no connection object defined. CursorType: adOpenForwardOnly - 0, adOpenKeySet - 1, adOpenDynamic - 2, adOpenStatic - 3 LockType: adLockReadOnly - 1, adLockPessimistic - 2, adLockOptimistic - 3, adLockBatchOptimistic - 4

4 Example of Recordset Source Table Name: –rs.open “customer”, “DSN=Sales” SQL: –rs.open “select * from customer”, “DSN = Sales” Stored procedure: –Rs.open “queryCustomer”, “DSN=Sales” Command Object –Rs.Open CmdObj

5 Command Objects Properties & Methods Command object properties: –ActiveConnection: Current connection –CommandType: adCmdText - 1, adCmdTable - 2, adCmdStoredProc - 4, etc. –CommandText: Table name if command type is adCmdTable, SQL statement if command type is adCmdText. Command object methods: –Execute

6 Example of Using Command Obj <% dim conn, cmd, adCmdTable, rs set conn=server.createObject("adodb.connection") conn.open "dsn=sales" set cmd=server.createObject("adodb.command") cmd.ActiveConnection=conn cmd.CommandText="customer" cmd.CommandType=2 adCmdTable=2 cmd.CommandType=adCmdTable Set rs= SeverCreateObject(“adodb.recordset”) Rs.open cmd %>

7 Creating a Recordset with a Forward Only cursor and a Read Only Lock <% Dim adOpenForwardOnly, adLoadReadOnly, RS adOpenForwardOnly=0 adLoadReadOnly=1 Set rs=server.createObject(“adodb.recordset) Rs.open “customer”, “dsn=sales”, adOpenForwardOnly, adLockReadOnly

8 Using VB Constants, p294 <% …

9 Recordset Find Method Find : p303 –Demo: findcust.htm

10 Processing Updates with ADO AddNew: p304 –Cursor and lock type –Demo:addcust.htm Update Delete

11 AddNew Example <% Dim RS set rs=server.createoBJECT("adodb.recordset") rs.open "customer","DSN=sales",adOpenKeyset,adlockoptimistic rs.addnew rs.fields("cid")=request.form("cid") rs.fields("cname")=request.form("cname") rs.fields("city")=request.form("city") rs.fields("rating")=request.form("rating") rs.update response.write "New customer:" & request.form("cname") & "added" %>

12 Update Example <% dim rs, custID custID=request.form("cid") set rs=server.createobject("adodb.recordset") rs.open "customer","dsn=sales",adOpenKeyset,adlockoptimistic rs.find "cid = ‘" & custID & “’” if rs.eof then response.write "record not found" else rs.fields("city")=request.form("city") rs.fields("rating")=request.form("rating") rs.update response.write "customer: " & request.form("cname") & "updated" end if rs.close %>

13 Delete Example, p320 Rs.find criteria If Rs.eof then response.write “record not found” Else rs.delete adAffectCurrent rs.update End if

14 Processing Updates with SQL INSERT INTO tablename (FIELDS) VALUES (values) Use Connection object’s Execute method to run update commands Demo: addCustConn.htm

15 Insert Into Example Dim conn set conn=server.createoBJECT("adodb.connection") conn.open "DSN=sales" strSQL="insert into customer (cid,cname,city,rating) values " strSQL=strSQL & "('" & request.form("cid") & "', '" & request.form("cname") strSQL=strSQL & "', '" & request.form("city") & "', '" & request.form("rating")& "')" response.write strSQL conn.execute(strSQL) response.write "New customer:" & request.form("cname") & "added" %>

16 Update Command UPDATE tablename SET field1=value1, field2=value2, … WHERE criteria

17 Delete Command DELETE * FROM tablename WHERE criteria


Download ppt "ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”"

Similar presentations


Ads by Google