Dot net database connectivity, (Data connection,Data adapter;Data Reader)


This article shows how to do database connectivity in .net

Description :


Now we will discuss about what is the level of database connectivity in .net(any like vb.net, asp.net, c#). Dot Net program allows us to connect with database,very easily (comparing other languages).
But it is not enough to interact with that one..
for instance we can calculate or execute queries by ADO.net but it is not at all helpful to interact. we can only execute some selected queries like "SELECT, PROCEDURE, INSERT, UPDATE,DELETE"only.
so we should manage with that one.

Example program of database connectivity program..


Imports System.Data.SqlClient

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As SqlConnection
con = New SqlConnection("server=server;Database=Master;user id=sa;password=killer")
con.Open()
MsgBox("Connection Opened")
con.Close()
End Sub
Imports System.Data.OleDb

Dim con As OleDbConnection
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/demo/vb.net/sampleDB.mdb")
con.Open()
MsgBox("Database Connected")
con.Close()
Dim con As New SqlConnection
Dim command As New SqlCommand
con = New SqlConnection("server=system8;database=master;user id=sa;password=killer")
command = New SqlCommand("Insert into Customertracking values('C006','Vidhya','Nandhini','23 KK Nagar',23345,'vidhya@rediff.com')", con)
Dim r As Integer
command.Connection.Open()
r = command.ExecuteNonQuery()
command.Connection.Close()
MsgBox("Record Inserted")
Dim custid As String
custid = InputBox("Enter Customer ID")
Dim con As New SqlConnection
Dim command As New SqlCommand
con = New SqlConnection("server=system8;database=master;user id=sa;password=killer")
command = New SqlCommand("Select * from customertracking", con)
Dim dr As SqlDataReader
command.Connection.Open()
dr = command.ExecuteReader()
While (dr.Read())
If (dr(0) = custid) Then
txtCustid.Text = custid
txtFname.Text = dr(1)
txtLname.Text = dr(2)
txtAddress.Text = dr(3)
txtPhone.Text = dr(4)
txtEmail.Text = dr(5)
End If
End While
command.Connection.Close()
End Sub




if u like this.. please leave a comment
Thank you
Ashokchakravarthi


Related Articles

Object Serialisation in dot net

This article provides an overview of the serialization used in Microsoft .NET and the usage of the same.For example, serialization is used to save session state in ASP.NET and to copy objects to the clipboard in Windows Forms. It is also used by remoting to pass objects by value from one application domain to another.

More articles: Dot Net

Comments

Author: vishnu16 Jan 2010 Member Level: Bronze   Points : 0

thats the easiest example to learn buddy ..

Author: ashok16 Jan 2010 Member Level: Bronze   Points : 0

thank you very much pras!..

Author: parashar09 Jul 2010 Member Level: Bronze   Points : 0

very good example for beginners

Guest Author: Aje17 Apr 2012

Thanks very for positing this



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: