Tuesday, December 25, 2007

Connection with Oracle and VB.NET

Connection with Oracle and VB.NET


In this section i show how connect with oracle and VB.NET


--------------------------------------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!---1---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--------------------------------------------------------------------------------------------
At first Design a form with .NET.


Print Screen



--------------------------------------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!---2---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--------------------------------------------------------------------------------------------
Add Module from new add Items .Then new module will be added .
Write main connection sting in this module
Example code:

Imports System.Data.OleDb 'Working with oledb data access
Module Module1
Public orada As OleDbDataAdapter 'Represents a set of data commands and a database connection that are
' ''used to fill the DataSet and update the data source.
Public orads As DataSet
Public oradrSQL As OleDbDataReader 'is used to read rows from source
Public oracnSQL As OleDbConnection 'Represent a unique connection to a database
Public oracmSQL As OleDbCommand 'Present a SQL statement to execute against a data source
Public orastrSQL As String
Public oraconnectionString As String = _
"Provider=msdaora;Data Source=dboracle;Persist Security Info=True;User ID=scott;Password=tiger;Unicode=True"

Public Sub mainConnection()
oracnSQL = New OleDbConnection(oraconnectionString)
oracnSQL.Open()
End Sub
Public Sub load_grid()
orada = New OleDbDataAdapter("Select * from table_info", oraconnectionString)
orads = New DataSet
orada.Fill(orads) 'Fill keeps data into dataset
FrmInfo.DataGridView1.DataSource = orads.Tables(0)
End Sub
End Module

--------------------------------------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!---3---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--------------------------------------------------------------------------------------------
Now this connection call from form

So that all SQL statement will executed like Save,Delete,Update,Search,Exit

Note: Connection must be call in Load event

Example code :


Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Public Class FrmInfo
Private Sub frmSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmSave.Click

orastrSQL = "insert into table_info values('" & _
TextBox1.Text & "', '" & _
TextBox2.Text & "', '" & _
TextBox3.Text & "', '" & _
TextBox4.Text & "', '" & _
TextBox5.Text & "', '" & _
TextBox6.Text & "', '" & _
TextBox7.Text & "')"


oracmSQL = New OleDbCommand(orastrSQL, oracnSQL)
oracmSQL.ExecuteNonQuery() 'perform insert,update or delete operations
MessageBox.Show("Saved Successfully")
load_grid()
End Sub


Private Sub FrmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mainConnection()
load_grid()
End Sub

Private Sub frmSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmSearch.Click

orastrSQL = "Select * from table_info WHERE (Name='" & Me.TextBox1.Text & "')"

oracmSQL = New OleDbCommand(orastrSQL, oracnSQL)
oradrSQL = oracmSQL.ExecuteReader()

If oradrSQL.Read() Then
TextBox1.Text = oradrSQL.Item(0).ToString
TextBox2.Text = oradrSQL.Item(1).ToString
TextBox3.Text = oradrSQL.Item(2).ToString
TextBox4.Text = oradrSQL.Item(3).ToString
TextBox5.Text = oradrSQL.Item(4).ToString
TextBox6.Text = oradrSQL.Item(5).ToString
TextBox7.Text = oradrSQL.Item(6).ToString
Else
MessageBox.Show("Data Not Found", "Serach", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub

Private Sub frmExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmExit.Click
Me.Close()
End Sub

Private Sub frmDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmDelete.Click
orastrSQL = "Delete from table_info where Name='" & TextBox1.Text & "'"

oracmSQL = New OleDbCommand(orastrSQL, oracnSQL)
oracmSQL.ExecuteNonQuery()
MessageBox.Show("Deleted Successfully")
load_grid()
End Sub

Private Sub frmUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmUpdate.Click

orastrSQL = "Update table_info set Age ='" & TextBox2.Text & "',Sex ='" & TextBox3.Text & " ', " _
& "Address ='" & TextBox4.Text & "'," & "Phone ='" & TextBox5.Text & "'," _
& "Email ='" & TextBox6.Text & "'," & "Nationality ='" & TextBox7.Text & "' where Name ='" & TextBox1.Text & "'"


oracmSQL = New OleDbCommand(orastrSQL, oracnSQL)
oracmSQL.ExecuteNonQuery()
MessageBox.Show("Updates Successfully")
load_grid()
End Sub
End Class

--------------------------------------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!---4---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--------------------------------------------------------------------------------------------
All SQL statement are declare above portion.


Print Screen


so lets try.

You can see DEMO from this link SAMPLE DEMO

_________________________________________________________________
Note: This Example is for applicable only desktop applications
_________________________________________________________________