Microsoft Access database is a Database Management System developed By Microsoft and it is very simple and easy to use for windows application and web application development with Dotnet . Platform. . It supports Microsoft Jet Database Engine . It supports Graphical User Interface with Professional Development tools.
Microsoft is well supported database to be used in Microsoft Visual Studio to develop applications. It is very easy to connect .net either vb.net or C#.net application with Microsoft Access Database
Microsoft is well supported database to be used in Microsoft Visual Studio to develop applications. It is very easy to connect .net either vb.net or C#.net application with Microsoft Access Database
DOWNLOAD this Demo Project from my gmail upload link :-
Steps to Make Microsoft Access Database:
1. Go to desired location you want to make your Database
2. Right click then click on new and
3. Then click Microsoft Access 2007 Database
4. Create Desired Table with required Fields
5. Save it as Microsoft Access database 2003 format
6. click ok
DOWNLOAD THIS Demo Database from my gmail upload link :-
Steps To Make This Demo Project :-
1. Start your Visual Studio 2010
2. At start page click on Create New Project
3. In Dialog Box provide Project name and path where to save this project and click ok
4. Now from Toolbox Drag four Textboxes
5. Drag 1 Button and make design as show in below image
Design of Form 1
6. Now copy the code given in code section below :-
Code of Form1
Imports System.Data.OleDb
Public Class Form1
Dim connection As New OleDbConnection
Dim command As New OleDbCommand
Dim ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\test.mdb"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
command.CommandText = "insert into info_table values (" & TextBox1.Text & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'," & TextBox4.Text & ")"
command.CommandType = CommandType.Text
command.Connection = connection
command.ExecuteNonQuery()
MsgBox("Record inserted ")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection.ConnectionString = ConnectionString
connection.Open()
End Sub
End ClassExplanation of Code
Imports System.Data.OleDb
This line of code tells we are importing Oledb Namespace. Oledb stands for Object Linking and Embedding . For using Access database with .net applications we have to inherit classes from this oledb Namespace . Oledb Namespace contains all classes that are required to connect vb.net/c# application to Microsoft Access
Database
End Class
Database
Public Class Form1
Dim connection As New OleDbConnection
Dim command As New OleDbCommand
Dim ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\test.mdb
Now In These lines of code we declared 3 things
1. OledbConnection :- OledbConnection is a class contained in System.Data.Oledb Namespace and it is used to make connectivity to Access Database . It receives the connection string that tells the path of Microsoft Access Database to connect to it. In this line of code we have created instance of Oledbconnection class
2. OledbCommand :- OledbCommand is a class contained in System.Data.Oledb Namespace and it is used to define or specify the command that we are going to execute on Microsoft Access Database. In this line of code we are creating instance of OledbCommand to use this instance in further code
3. Connection string :- Now we are creating a variable named connectionString that will receive the string or path that tell how we connect to our Access database. It receives two parameters :-
Provider=Microsoft.Jet.OLEDB.4.0 --
Data Source=" & "C:\test.mdb
Now Provider is main part here in connection string . Provider is different for different approaches used for connecting data to database . The Connection string contains information that the Provider needs to know to connect to database Once connected then rest of job is done by provider
End Sub
Now Provider is main part here in connection string . Provider is different for different approaches used for connecting data to database . The Connection string contains information that the Provider needs to know to connect to database Once connected then rest of job is done by provider
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection.ConnectionString = ConnectionString
connection.Open()
End Sub
Now in this block of code we have initialized our OledbConnection instance with connection string variable that we have initialized in previous step. So in this code we have given the Oledbconnectin instance means connection some information about Provider to use and location of database file on computer
Then we have used connection.open() method opens port for Enter into Access Database. this opens a pipe to execute query in database
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
command.CommandText = "insert into info_table values (" & TextBox1.Text & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'," & TextBox4.Text & ")"
command.CommandType = CommandType.Text
command.Connection = connection
command.ExecuteNonQuery()
MsgBox("Record inserted ")
End Sub
Now at last we have used insert query that is coded on Button Click Event . In this code I have told which commandtext to choose and what will be commandType test or storeprocedure . and also we have initialized the command with Oledbconnection instance. Then we have execute command using ExecuteNonQuery() method. This method returns the number of rows affected the database