Creating A Connection: Imports
Creating A Connection: Imports
NET 2010
CREATING A CONNECTION
1. Create your DATABASE first with the needed TABLES.
2. Create or Add a MODULE in Visual Basic .net.
3. Type this line of code to the upper most part of the code window of your Module.
Imports System.Data.Oledb
Note: You also need to type this line of code in ALL OF YOUR FORMS.
4. Declare a variable that will store the database path for your connection.
Public con As OledbConnection = New OledbConnection("Connection String")
Note: CONNECTION STRING should be replaced by the actual connection string of your database.
Sumpay daw sa code: CONNECTION STRING: Provider = Microsoft.Jet.OleDB.4.0;Data Source =
5. You might also need to declare the following variables for future use.
Public ds As New DataSet
Public cmd As OledbCommand
Public dr As OledbDataReader
Public da As OledbDataAdapter = New OledbDataAdapter
Public sqlSearch, sqlDelete, sqlInsert, sqlUpdate As String
DEFINITION OF TERMS:
o OledbConnection
The classes you can use to connect your application to a Microsoft Access database are stored in the
System.Data.OleDb namespace.
To access the database, the first action you take consists of establishing a connection with the database. To
support this, the System.Data.OleDb namespace provides the OleDbConnection class.
o ConnectionString - is used to specify how the connection would be performed. It is a string made of
different sections. Everything in this string is case-insensitive.
o DataSet
The DataSet can then be treated like a database in your program code.
A DataSet can contain all the basic elements of a database: tables, keys, indexes, and even relations
between tables. So by creating a DataSet, you'll be discovering the structure of a database at the same
time. The fundamental differences between a DataSet and a database are that a database generally resides
on a hard drive in one or more files and is usually larger. A DataSet usually holds a subset of the data in a
full database.
o OledbCommand
After establishing a connection to a database, you can perform actions or operations on it. To make this
possible and probably easier, the System.Data.OleDb namespace provides the OleDbCommand class.
As its name suggests, this class is used to create commands or actions to be performed on the database.
o OledbDataReader
DataReader Object in ADO.NET is a stream-based, forward-only, read-only retrieval of query results from
the Data Source, which do not update the data. The DataReader cannot be created directly from code; they
created only by calling the ExecuteReader method of a Command Object.
DataReader Object provides a connection oriented data access to the data Sources.
o OledbDataAdapter
OleDbDataAdapter is a part of the ADO.NET Data Provider and it resides in the System.Data.OleDb
namespace. OleDbDataAdapter provides the communication between the Dataset and the OleDb Data
Sources. We can use OleDbDataAdapter Object in combination with Dataset Object.
Try Catch
The Try word means "Try to execute this code". The Catch word means "Catch any errors here". The ex is a
variable, and the type of variable it is is an Exception object.
For more details, check this site: http://www.homeandlearn.co.uk/net/nets5p4.html
Sources: http://www.functionx.com/vbnet/oledb/Lesson01.htm
http://vb.net-informations.com/ado.net-dataproviders/ado.net-datareader.htm
6. Create a function that will identify whether the connection is open or not.
Public Sub open_con()
If Not con.State = ConnectionState.Open Then
Try
con.Open()
Catch ex As Exception
MsgBox("Cannot find Server")
End Try
End If
End Sub
Note: All DECLARATIONS (variables and etc) and FUNCTIONS can be written in the MODULE.
Try
cmd.ExecuteNonQuery()
MsgBox("Successfully Saved!", MsgBoxStyle.Information, "MESSAGE")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
End Try
cmd = Nothing
sqlInsert = Nothing
con.Close()
2. Don’t forget to Type this line of code to the upper most part of the code window of your FORM.
Imports System.Data.Oledb
UPDATING EXISTING RECORD IN THE DATABASE
Try
cmd.ExecuteNonQuery()
MsgBox("Record successfully updated!", MsgBoxStyle.Information, "MESSAGE")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
End Try
cmd = Nothing
con.Close()
2. Don’t forget to Type this line of code to the upper most part of the code window of your FORM.
Imports System.Data.Oledb
2. Don’t forget to Type this line of code to the upper most part of the code window of your FORM.
Imports System.Data.Oledb
dr.Close()
dr = Nothing
cmd = Nothing
sqlsearch = Nothing
con.Close()
2. Don’t forget to Type this line of code to the upper most part of the code window of your FORM.
Imports System.Data.Oledb
SAMPLE CODE:
open_con()
da = New OledbDataAdapter("Select * FROM tblcustomer where customerid Like '" &
txtSearch.Text & "%'", con)
ds = New DataSet
da.Fill(ds, "tblcustomer")
dgcustomer.DataSource = ds.Tables("tblcustomer")
ds = Nothing
da = Nothing
con.Close()
While dr.Read
lsvCustomer.Items.Add(dr("CustomerID"))
lsvCustomer.Items(lsvCustomer.Items.Count - 1).SubItems.Add(dr("Firstname"))
lsvCustomer.Items(lsvCustomer.Items.Count - 1).SubItems.Add(dr("MiddleName"))
lsvCustomer.Items(lsvCustomer.Items.Count - 1).SubItems.Add(dr("LastName"))
lsvCustomer.Items(lsvCustomer.Items.Count - 1).SubItems.Add(dr("Landline"))
lsvCustomer.Items(lsvCustomer.Items.Count - 1).SubItems.Add(dr("MobileNo"))
End While
dr.Close()
dr = Nothing
cmd = Nothing
con.close()