Saturday 7 September 2013

Access database with vb.net


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

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 Class


Explanation 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




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
End Class


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 S
ystem.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


    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

Delete Record from Access Database

Share this

12 Responses to "Access database with vb.net"

  1. Hi,
    Please help me to solve error at command.ExecuteNonQuery().

    System.Data.OleDb.OleDbException was unhandled
    ErrorCode=-2147467259
    HResult=-2147467259
    Message=Operation must use an updateable query.
    Source=Microsoft JET Database Engine
    StackTrace:
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
    at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
    at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\OM\Downloads\WindowsApplication3-2014-02-21\WindowsApplication3\Form1.vb:line 11
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(ApplicationContext context)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:

    Thanks
    Gourav

    ReplyDelete
  2. Please post your code .......so that i can solve the error .......

    ReplyDelete
  3. What's up friends, good post and pleasant arguments commented here,
    I am in fact enjoying by these.

    Here is my weblog; skylights Cape Town ()

    ReplyDelete
  4. Thanks for you great appreciation

    ReplyDelete
  5. Does anyone have any idea of how to create the database in the first place without having to set it up in Access first.

    ReplyDelete
  6. What's Happening i'm new to this, I stumbled upon this I have discovered It positively useful
    and it has aided me out loads. I hope to give a contribution &
    assist other customers like its aided me.
    Good job.

    My webpage; Cape Town skylights

    ReplyDelete
  7. Hi, I do believe this is an exceplent website. I stumbledupon it
    ;) I'm going to return yet again since I bookmarked it.
    Money and freedom is the greatest way to change, may you be rich and continue to help others.


    my page: battlefield 4 campaign cheats pc

    ReplyDelete
  8. Its like you read my mind! You seem to know soo much about this, like you wrote the book in it or something.
    I think that you can do with a few pics to drive the messsage home a little bit, but
    other than that, this is wonderful blog. A fantastic read.
    I will certainly be back.

    Stop by my website - battlefield 4 cheats online

    ReplyDelete
  9. My brother suggested I might like this website.
    He was entirely right. This post actually made myy day.
    You can not imagine just how much time I had spentt for this info!

    Thanks!

    My webpage; battlefield 4 cheats ps4 multiplayer

    ReplyDelete
  10. A motivating discussion iss definitely wprth comment. There's noo doubt that that you should publish more about this issue, it might not be a taboo matter but generally folks
    don't discuss such topics. To the next! Cheers!!

    Reviuew my web site ... battlefield 4 ps3 ccheat pkg ()

    ReplyDelete
  11. A fascinating discussion is deinitely worth comment.

    I do think that yoou need too write ore about this topic, it may not bbe a taboo matter but
    uszually people don't discuss these subjects. To the next!
    Kind regards!!

    Heere iis my web site battlefield cheats for ps3

    ReplyDelete