Matakuliah Tahun Versi : T0063/Pemrograman Visual : 2005 : 1/0 Pertemuan 10 Pemrograman Database 1 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Mendemonstrasikan penggunaan database yang ditunjang oleh pemrograman visual (C3) 2 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 3 Database • Akses database – ODBC – DAO – RDO – ADO 4 ADO • ActiveX Data Object (ADO) – Generasi paling baru untuk mengakses data – Terdiri : • Consumer • Service • Data provider 5 ADO ADO Object Model • Connection – Error – Property • Command – Parameter – Property • Recordset – Fields – Property 6 Connection 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 7 Connection String Argument Argument Description Data Source The 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). DSN An ODBC source name registered on the current machine; this argument can replace the Data Source argument. Filename A file that contains information about the connection; this argument can be an ODBC DSN file or a Microsoft Data Link (UDL) file. Initial Catalog The name of the default database. When connecting to an ODBC source, you can also use the Database argument. Password The 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. Provider The name of the OLE DB provider; the default value is MSDASQL, the provider for ODBC sources. User ID The user's name. When connecting to an ODBC source, you can use the UID argument instead. 8 State Value Description 0-adStateClosed The connection is closed. 1-adStateOpen The connection is open. 2-adStateConnecting The connection is being opened. 4-adStateExecuting The connection is executing a command. 8-adStateFetching A Recordset is being retrieved. 9 Command Command • Characteristics – Berisi sintak SQL dan parameter untuk dieksekusi • Properties – CommandText – ActiveConnection – Parameters • Events – Tidak ada • Methods – Execute – Cancel 10 RecordSet RecordSet • Characteristics – Interface data • Properties – – – – – – • Events – – – – – • Source ActiveConnection DataSource Filter CursorLocation (2-adUseServer atau 3-adUseClient ) CursorType FetchProgress, FetchComplete WillMove, MoveComplete EndOfRecordset WillChangeField, FieldChangeComplete WillChangeRecord, RecordChangeComplete Methods – Open 11 Cursor Type Value Description 0-adOpenForwardOnly Server side cursor, you can navigate a forwardonly Recordset only by using the MoveNext method 1-adOpenKeyset Keyset cursors are similar to dynamic cursors, but they don't include records added by other users 2-adOpenDynamic Dynamic 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-adOpenStatic Actually a copy of the data coming from the database. Static cursors create a fully scrollable snapshot of all the records 12 Fields Fields • Cara mengakses field : – rs.Fields(Index).Value – rs.Fields(“FieldName”).Value – rs.!FieldName 13 Connection in Action • Persiapan – Database – Reference 14 Connection in Action • Connection – Deklarasi object Connection – Tentukan Data Provider – Tentukan spesifikasi sumber data – Open Connection 15 Connection in Action Koneksi ke Access Koneksi ke SQLServer 16 Command in Action • Command – Deklarasi object Command – Hubungkan command dengan koneksi aktif – Tentukan spesifikasi query data – Jalankan command 17 Command in Action 18 RecordSet in Action • RecordSet – Deklarasi object recordset – Jalankan command dan tampung ke dalam recordset 19 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 20 RecordSet in Action • Bandingkan hasilnya dengan program sebelumnya 21 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 22 Query Data • Loop – Recordset berisi collection data, sehingga untuk mengaksesnya diperlukan loop Do List1.AddItem rs.Fields(0).Value rs.MoveNext Loop Until rs.EOF – atau If rs.RecordCount <> 0 Then rs.MoveFirst Do List1.AddItem rs.Fields(0).Value rs.MoveNext Loop Until rs.EOF End If 23 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=? 24 Modifying Data 25 Modifying Data Memodifikasi record tunggal • AddNew – Menambah record baru rs.AddNew rs!fname = "Nana" rs!lname = “Sylva" rs.Update • Delete – Menghapus record aktif • Modify – Memodifikasi record aktif rsAuthors.Delete rs!fname = “Nana" rs!lname = “Sylva" rs.Update 26 Modifying Data • Contoh : 27