vb.net database connection basics part 1

In this guide, I’m going to show you the things you should know when you want to connect  your vb.net application to a database.

Connecting your application to any database is easy as long as you know the basics. Because almost all of the codes used in connecting to different database are the same. You only have to modify a few lines of code to make it work for you.

The connection strings is one of those that needs to be modified. And you can find a lot of connection strings on connectionstrings.com. They have connection strings for every database that you can think or know of.

This guide is the one that you might want to check out first before trying any of these:

Yeah, that is some sort of a summary of all the things I have discussed regarding database connectivity on vb.net.


Creating a new project

Let’s get to the point. First thing that you’ll need to know is creating a new project in visual studio. If you find this lame, then you can just skip and scroll down until you find what you need. Because I’m doing this for the absolute noob’s sake.


Click on new, and then project, then you’ll see something like this. Just make sure that you have selected the windows form application.  Then press ok.



Creating and class

Second thing that you need to know is creating new classes. Because we will be using this new class as a base class for inheriting and using methods that we will use to connect and manipulate the database.



Declaring an object of a class

Next thing that you’ll gonna need to know is how to declare an object of the newly created class in your windows forms (the one with the graphical user interface, or an empty canvas). Just to be sure, I’m referring to this one:


To declare an object of the class in this form, just double click it so that you’ll see its source code.

Then you type this line of code, just below the public class form1.

Dim c1 As New Class1

Of course the ‘Class1’ here can vary. Depending on what you have named your class. Good news though, you can see what’s your class name on the solution explorer.



Creating subroutines

Fourth thing that I want to explain to you is how you can create subroutines. This is important since were going to use subroutines or subs for querying the database. And its more convenient using subs so you wont be copy pasting the code all over when you need something that needs to be repeated.

Just go back to the class, and type this one:

Public Sub insert_records()
End Sub

The whole code will now look like this:


That’s how to create a sub. And what you’ll be putting inside those subs are individual queries like inserting, updating, and deleting records from the database. Each sub will have different functions and parameters. But there can only be one function that each one performs so that it won’t become confusing.


Creating functions

Functions are different from subs, since subs only perform an action. A function on the other hand, performs an action and then returns a value to the one who is calling it.

I often use functions on reading data from a database using the built in data reader. Here’s an example of a function which reads data from a database. It uses mysql stored procedures that’s why the code is a bit different.

Public Function readfrom_drug_type_table() As MySqlDataReader
           cmd.CommandText = "CALL readfrom_drug_type_table"
           MsgBox("Reader is opened close it first by clicking on the close link")
       End Try
       Return cmd.ExecuteReader
End Function

Always remember that when you’re creating functions you must always have a return value. You will get an error if you don’t have it. In the example, the return value is ‘cmd.executeReader’.


Calling subroutines

I often call subroutines from windows forms. And to call subroutines, you must have an object of the class where the subroutine is declared. And the subroutine should also be public so that any class can access it. Scroll up if you have forgotten how to create an object of a class. If you remembered, the name of the object I declared at the top is ‘c1’ referencing the class named ‘class1’. To call the subroutine ‘insert_records’ from the form load event you’ll just have to double-click on the form that you wish to be called it from then type this line of code.


Make the necessary changes if your object is not called ‘c1’ and your subroutine is not called ‘insert_records’



That’s it for part1, I hope you learned something. And please share this post using the facebook share buttons and stumbleupon buttons below. ThanksSmile

One thought on “vb.net database connection basics part 1

  1. Pingback: vb.net database connection basics part 2 « Data Integrated Entity

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s