How to Connect a Database and Add/Update/Delete/Record

Only available on StudyMode
  • Download(s) : 123
  • Published : March 21, 2013
Open Document
Text Preview
How to Connect to a Database and Add/Update/Delete Record

In this tutorial I will explain to you on how to connect to an Access database and allow you to Add/Update/Delete a record. To fully understand these tutorials please download the source code How to Add/Update/Delete Record using MS Access Database. This source code is part of the Hotel Reservation System that I am currently working. At the end of this tutorial you will learn the basic of database programming. I would like, however, to emphasize especially for beginners that one way to learn programming is to know how to debug a program and devote some of your time to reading. Don't be frightened on how short or long an article should be. The important is at the end of the tutorial you will learn something NEW! If you already know the topic, then don’t bother to study this again. Table of Contents

1. Introduction
2. Let’s get started
3. Database Connection
4. Add and Update a Record
5. Delete a Record
6. Final Thoughts
Introduction
Before I started learning VB.NET one of the topic that I search for in the internet is on how to connect to the database and make some changes to the table. Although there’s a lot of results, but I cannot find one that suit to my needs. Most of the tutorial is using drag and drop features of vb.net editor. Well, this is okay in most cases but what if you’d like to manipulate the data by code? So, I created this tutorial so that beginner programmer will learn from this. Let’s get started

It is very important that you use your common sense to understand the logic of database programming. There’s a lot of features built-in to Visual Basic Editor that most programmer especially beginner who overlook it. One of the favorite tools I usually used is the DEBUGGER. If you only knew how important a debugger is, then you do not even need to study this tutorial. Why? Because you can jump right away to the source code and start firing the F8 command from your keyboard and analyze every line as you step through the code. Anyway beginner is a beginner. You need to start from scratch. If you have already downloaded the source code, then open it in the visual basic .net editor by double clicking the “HowtoAddUpdateDeleteRecord.sln”. If you want to know what is the object that runs the first time you start the program (by pressing F5) then double click the “My Project” at the Solution Explorer. Look at the Startup Form. You will see that the value is “frmCustomersList”. Now, click this object in the Solution Explorer and click the View Code at the toolbar. Look for the Load event similar below: Private Sub frmCustomersList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load         sSql = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address FROM Customers ORDER BY CustomerID ASC"         Call FillList()

        FillListView(lvList, GetData(sSql))
End Sub
frmCustomersList_Load is the second procedure that runs when you hit the F5 Key from your keyboard. If you’d like to know how this code is executed then press F8. Believe it or not F8 is the answer to all your programming question. And I really mean it. When I started programming all I do is to search for free source code and start using the debugging tool. That’s why Visual Basic is being named as Rapid Application Development or RAD. If you follow the debugger the first line it executes is the Private Sub frmCustomersList_Resize(ByVal senderAs Object, ByVal e As System.EventArgs) then followed by frmCustomersList_Load which is actually the important procedure to note here. Another important debugging tool is “Toggle Breakpoint”. You will be prompted to your code if one of the line is marked by toggle break point. This can be done by pressing the F9 key or clicking the Debug menu then Toggle Breakpoint. This tool is important if the form is already loaded and you want to tract the execution of a code say within a command...
tracking img