2.7.11

Create Database Table with VB.NET


Hi Everybody,
I would like to discuss one of the most common FAQs regarding how we can create access database table with VB.NET 2010 Windows application code line? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-)We are creating a new Windows Application Project (database_table_create)



2-)We are designing this windows form in a similar way to the picture below



3-)We are writing this code

Imports System.Data
Imports System.Data.OleDb
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'connection string
Dim my_connection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database\customers.mdb"

'query string
Dim my_query As String = "CREATE TABLE new_database ( Customer_Number Counter," & _
"Customer_Name TEXT(50) NOT NULL , Customer_Surname TEXT(40))"

'create a connection
Dim my_dbConnection As New OleDbConnection(my_connection)

'create a command
Dim my_Command As New OleDbCommand(my_query, my_dbConnection)

'connection open
my_dbConnection.Open()

'command execute
my_Command.ExecuteNonQuery()

'close connection
my_dbConnection.Close()



End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'connection string
Dim my_connection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database\customers.mdb"

'insert (query string)
Dim my_query As String = "insert into new_database ( Customer_Name, Customer_Surname )" & _
" values ( 'jen' , 'Lte')"

'create a connection
Dim my_dbConnection As New OleDbConnection(my_connection)

'create a command
Dim my_Command As New OleDbCommand(my_query, my_dbConnection)

'connection open
my_dbConnection.Open()

'command execute
my_Command.ExecuteNonQuery()

'close connection
my_dbConnection.Close()
End Sub
End Class


4-)Lets run application and firstly click database table create button secondly insert sample data button.




5-) Lets check access database table


Have a great day
Yener Turkeli

3 comments:

  1. Outstanding =)))
    I was looking for something like that

    cheers...

    ReplyDelete
  2. Thank You so much for such a wonderful article ..Here I've question
    "How do we create separate database and Separate Tables to the individual Users ?"It would be really appreciate and really great full Yener.
    Once again thank you

    ReplyDelete
  3. * Complete Explanation of Access Db connectivity with vb.net
    * Download Demo App of Access and vb.net connectivity
    http://geeksprogrammings.blogspot.com/2013/09/access-database-with-vbnet.html

    ReplyDelete