* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download ActiveX Data Control
Microsoft Access wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Relational model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Database model wikipedia , lookup
Versant Object Database wikipedia , lookup
ActiveX Data Object ISYS 562 ADO • An ActiveX control ActiveX is build upon COM, a contract that defines a standard interface by which objects communicate. • ADO works with several different DBMSs. – Can use ODBC DSN. A Client/Server Access Application Request Access Application Database Server Response Using ODBC • Windows 2000/2003: • Control Panel /Administrative Tools/DataSource(ODBC) • Three types of data source names – System DSN Using ADO • Must reference ADO objects. In VBE: – Tools/Reference/ • click Microsoft ActiveX Data Object library 2.7 ADO Object Model • There are three main objects on which the ADO object models is built: – Connection object: make connection to the database. – Command object: run SQL against the database. – Recordset object: holds some or all of the records in database. ADO Programming Object Model Connection Errors Error Command Parameters Parameter RecordSet Fields Field Connection with a Connection Object • A connection object represents a unique session with a data source. • Connection string: database, OLE DB provider, password, if any, security, etc. • Use the Open method to open a connection. • Use the Execute method to execute SQL. • Manage transaction: BeginTrans, CommitTrans, rollBackTrans. Providers • MSDASQL – Microsoft OLE DB Provider for ODBC • Microsoft.Jet.OLEDB.4.0 • MSDAORA – For Oracle • SQLOLEDB – Microsoft SQL Server Connecting String • Containing information about database, OLE DB provider, password, if any, security, etc. • For Jet database: – Dim cn As ADODB.Connection – Set cn = New ADODB.Connection – cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;") ODBC DSN Connection • When using the ODBC provider, the connection string can be a DSN. For example: – Dim Cn as ADODB.Connection – Set Cn = New ADODB.Connection – cn.Open ("provider=msdasql;dsn=mySalesDB") Connection Object Methods • OPEN cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;") Close • Execute: – Return a recordset after executing a SQL Select statement. – Usually is used to execute SQL Insert and Delete that don’t return set of records. • Demo Command Objects Properties & Methods • Command object properties: – ActiveConnection: Current connection – CommandType: adCmdText, adCmdTable, adCmdStoredProc, etc. – CommandText: Table name if command type is adCmdTable, SQL statement if command type is adCmdText. • Command object methods: – Execute Creating ADO Recordset Objects • Connection object’s Execute method • Command object’s Execute method • Recordset’s Open method Types of Recordset Creating Recordset Using Connection ForwardOnly Option Explicit Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Private Sub Form_Load() Set cn = New ADODB.Connection cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;") Set rs = cn.Execute("select * from customer", adCmdText) Creating Recordset Using Command ForwardOnly Option Explicit Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim cm As ADODB.Command Private Sub Form_Load() Set cn = New ADODB.Connection cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.MDB;") Set cm = New ADODB.Command cm.CommandType = adCmdText cm.CommandText = "select * from customer“ Set cm.ActiveConnection = cn Set rs = cm.Execute Text1 = rs.Fields(0) Text2 = rs.Fields(1) End Sub Creating Recordset Using Recordset’s Open Method (Can open any type of recordset) Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open ("provider=msdasql;dsn=mySalesDB") Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "customer", cn, adOpenKeyset ‘Table or SQL Dim rc As Integer Do While Not rs.EOF rc = rc + 1 rs.MoveNext Loop MsgBox (rc) Recordset Properties • • • • • • • • • BOF, EOF BookMark CursorLocation CursorType LockType MarshalOption MaxRecords RecordCount Sort Recordset Methods • AddNew, Update, Delete • Find: Search a recordset. • MoveFirst, MoveLast, MoveNext, MovePrevious • Requery: Re-Execute the query. • GetRows: Fetch records to an array. Recordset Events • • • • FetchProgress, FetchComplete WillChangeField, FieldChangeComplete WillMove, MoveComplete WillChangeRecord, RecordChangeComplete • WillChangeRecordset, RecordsetChangeComplete • EndOfRecordSet Using SQL with ADO • SQL Select commands: – Connection object’s Execute method – Command object’s Execute method – RecordSet object’s Open • SQL Update commands: Insert, Delete, Update – Connection object’s Execute method • Cn.Execute “Update EmpFile Set Salary = Salary*1.05” – Command object’s Execute method sqlSTr = " Update EmpFile Set Salary = Salary*1.05” cm.CommandType = adCmdText cm.CommandText = sqlSTr cm.ActiveConnection = cn cm.Execute Private Sub Form_Load() Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;") 'cn.Open ("provider=msdasql;dsn=mySalesDB") Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "select cid from customer", cn, adOpenKeyset Do While Not rs.EOF List0.AddItem (rs.Fields("cid")) rs.MoveNext Loop End Sub Private Sub List0_Click() Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;") 'cn.Open ("provider=msdasql;dsn=mySalesDB") Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "select cname from customer where cid = '" & List0 & "'", cn, adOpenKeyset MsgBox (rs.Fields(0)) • • • • • • • • • Dim rating As String If Frame13 = 1 Then rating = "A" ElseIf Frame13 = 2 Then rating = "B" Else rating = "C" End If db.Execute ("update customer set rating='" & rating & "' where cid='" & List0 & "'") • Text9 = rating Recordset’s AddNew – A form with unbound textboxes is initialized for input. – After entering data, a Save/Add button is clicked and the event handler uses a pair of AddNew and Update methods to insert record. • Note: We can also use SQL Insert Unbound Control Input Example Private Sub Command8_Click() Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;") 'cn.Open ("provider=msdasql;dsn=mySalesDB") Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "customer", cn, adOpenDynamic, adLockOptimistic With rs .AddNew .Fields("cid") = Text0 .Fields("cname") = Text2 .Fields("city") = Text4 .Fields("rating") = Text6 .Update End With End Sub Find Method • Syntax: adoRecordsetName.Find criteria • Only accept simple comparison criteria. • Example: – recordset.Find “CID = ‘” & txtCID & “’” • Finding employees hired between 2/15/2000 and 5/15/2000: – Adodc1.recordset.Find “Hdate > #2/15/2000#” Then use IF statement in program to filter out those hired after 5/15/2000 Note: Search starts from the current position. To search from the beginning, must do: MoveFirst If no match is found, the EOF of the recordset will be set to True. Note: The search starts from the current position of the recordset. To start from the beginning, use the MoveFirst method. Find Code Example Dim searchID As String searchID = InputBox("enter searchID:") rs.MoveFirst rs.Find "cid='" & searchID & "'" If Not rs.EOF Then Text0 = rs.Fields("cid") Text2 = rs.Fields(1) Else MsgBox ("not found") End If Demo Using the BookMark • Bookmark is a variant type property that keeps the current recordset’s position. This position can be saved in a variant variable and later go to that position. BookMark Example To save bookmark: Dim BkMark As Variant Private Sub Command5_Click() BkMark = rs.Bookmark End Sub To go to the saved bookmark location: Private Sub Command6_Click() rs.Bookmark = BkMark Text0 = rs.Fields("cid") Text2 = rs.Fields("cname") End Sub