1 Pertemuan 10 Pemrograman Database Matakuliah: T0063/Pemrograman Visual Tahun: 2005 Versi: 1/0
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan penggunaan database yang ditunjang oleh pemrograman visual (C3)
3 Outline Materi Pemrograman Database Database ADO Connection Connection String Argument State Command RecordSet Cursor Type Fields Connection in Action Command in Action RecordSet in Action Processing Data Query Data Modifying Data
4 Database Akses database –ODBC –DAO –RDO –ADO
5 ADO ActiveX Data Object (ADO) –Generasi paling baru untuk mengakses data –Terdiri : Consumer Service Data provider
6 ADO ADO Object Model Connection –Error –Property Command –Parameter –Property Recordset –Fields –Property
7 Connection Characteristics –Mewakili satu session dengan data source Properties –ConnectionString –Provider –ConnectionTimeOut (default 30 detik) –CommandTimeOut (default 30 detik) –CursorLocation (adUseClient atau adUseServer) –State –Errors Events –WillConnect, ConnectComplete, Disconnect –WillExecute, ExecuteComplete, Methods –Open –Execute
8 Connection String Argument ArgumentDescription Data SourceThe name of the SQL Server or the name of the MDB database to which you want to connect. When connecting to an ODBC source, this argument can also be the name of a Data Source Name (DSN). DSNAn ODBC source name registered on the current machine; this argument can replace the Data Source argument. FilenameA file that contains information about the connection; this argument can be an ODBC DSN file or a Microsoft Data Link (UDL) file. Initial CatalogThe name of the default database. When connecting to an ODBC source, you can also use the Database argument. PasswordThe user's password. When connecting to an ODBC source, you can use the PWD argument. You don't need to pass your user ID and password if you're connecting to SQL Server and you use integrated security. Persist Security Info True if ADO stores the user ID and the password in the data link. ProviderThe name of the OLE DB provider; the default value is MSDASQL, the provider for ODBC sources. User IDThe user's name. When connecting to an ODBC source, you can use the UID argument instead.
9 State ValueDescription 0-adStateClosedThe connection is closed. 1-adStateOpenThe connection is open. 2-adStateConnectingThe connection is being opened. 4-adStateExecutingThe connection is executing a command. 8-adStateFetchingA Recordset is being retrieved.
10 Command Characteristics –Berisi sintak SQL dan parameter untuk dieksekusi Properties –CommandText –ActiveConnection –Parameters Events –Tidak ada Methods –Execute –Cancel
11 RecordSet Characteristics –Interface data Properties –Source –ActiveConnection –DataSource –Filter –CursorLocation (2-adUseServer atau 3-adUseClient ) –CursorType Events –FetchProgress, FetchComplete –WillMove, MoveComplete –EndOfRecordset –WillChangeField, FieldChangeComplete –WillChangeRecord, RecordChangeComplete Methods –Open
12 Cursor Type ValueDescription 0-adOpenForwardOnlyServer side cursor, you can navigate a forward- only Recordset only by using the MoveNext method 1-adOpenKeysetKeyset cursors are similar to dynamic cursors, but they don't include records added by other users 2-adOpenDynamicDynamic cursors consist of a set of bookmarks to the actual data in the data source, automatically updated when other users add or delete a record or change any record already in the Recordset 3-adOpenStaticActually a copy of the data coming from the database. Static cursors create a fully scrollable snapshot of all the records
13 Fields Cara mengakses field : –rs.Fields(Index).Value –rs.Fields(“FieldName”).Value –rs.!FieldName
14 Connection in Action Persiapan –Database –Reference
15 Connection in Action Connection –Deklarasi object Connection –Tentukan Data Provider –Tentukan spesifikasi sumber data –Open Connection
16 Connection in Action Koneksi ke Access Koneksi ke SQLServer
17 Command in Action Command –Deklarasi object Command –Hubungkan command dengan koneksi aktif –Tentukan spesifikasi query data –Jalankan command
18 Command in Action
19 RecordSet in Action RecordSet –Deklarasi object recordset –Jalankan command dan tampung ke dalam recordset
20 RecordSet in Action Cara Lain Akses Data * Untuk mempersingkat perintah, biasanya Command jarang dipakai Execute dilakukan langsung oleh connection, tanpa melalui command, dan ditampung oleh recordset *Masih banyak cara lainnya, coba eksplor cara akses data cukup menggunakan recordset saja
21 RecordSet in Action Bandingkan hasilnya dengan program sebelumnya
22 Processing Data Struktur Navigasi Recordset –BOF, penanda awal record –EOF, penanda akhir record –MoveFirst, kembali ke posisi awal –MoveLast, maju ke posisi akhir –MovePrevious, kembali ke posisi sebelumnya –MoveNext, maju ke posisi sesudahnya –RecordCount, mengetahui jumlah record
23 Query Data Loop –Recordset berisi collection data, sehingga untuk mengaksesnya diperlukan loop –atau Do List1.AddItem rs.Fields(0).Value rs.MoveNext Loop Until rs.EOF If rs.RecordCount <> 0 Then rs.MoveFirst Do List1.AddItem rs.Fields(0).Value rs.MoveNext Loop Until rs.EOF End If
24 Modifying Data Modifying Record –Banyak record, menggunakan sintaks SQL –Satu record, menggunakan recordset Sintaks SQL –Select –Insert –Update –Delete SELECT * FROM table1 WHERE field=? INSERT INTO table1 (field1, field2) VALUES (newValue1, newValue2) UPDATE table1 SET field=newValue DELETE FROM table WHERE field1=?
25 Modifying Data
26 Modifying Data Memodifikasi record tunggal AddNew –Menambah record baru Delete –Menghapus record aktif Modify –Memodifikasi record aktif rs.AddNew rs!fname = "Nana" rs!lname = “Sylva" rs.Update rsAuthors.Delete rs!fname = “Nana" rs!lname = “Sylva" rs.Update
27 Modifying Data Contoh :