Senin, 18 Mei 2009

ADONET DATASET tables OLEDB

The DataSet contains DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The data inside Table is in the form of Rows and Columns . The OleDbDataAdapter object allows us to populate DataTables in a DataSet.

In some situations we have to find how many tables inside the Dataset Object contains . The following VB.NET source code shows how to find the tables inside the Dataset.

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"

sql = "Your SQL Statement Here"

You have to replace the string with your real time variables.


Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As OleDbConnection
Dim oledbAdapter As OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String
Dim i As Integer

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
sql = "Your SQL Statement Here"

connection = New OleDbConnection(connetionString)
Try
connection.Open()
oledbAdapter = New OleDbDataAdapter(sql, connection)
oledbAdapter.Fill(ds, "OLEDB Temp Table")
oledbAdapter.Dispose()
connection.Close()

For i = 0 To ds.Tables.Count - 1
MsgBox(ds.Tables(i).TableName)
Next

Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class

ADONET DATASET SQLSERVER

The DataSet contains the copy of the data we requested through the SQL statement. We can use Dataset in combination with SqlDataAdapter class . The SqlDataAdapter object allows us to populate Data Tables in a DataSet. We can use Fill method in the SqlDataAdapter for populating data in a Dataset.

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

sql = "Your SQL Statement Here"

You have to replace the string with your real time variables.

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim i As Integer
Dim sql As String

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Your SQL Statement Here"

connection = New SqlConnection(connetionString)

Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()

For i = 0 To ds.Tables(0).Rows.Count - 1
MsgBox(ds.Tables(0).Rows(i).Item(0) & " -- " & ds.Tables(0).Rows(i).Item(1))
Next

Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class

ADONET DATASET relations

The DataSet contains DataTableCollection and their DataRelationCollection. The DataSet.Relations property is an instance of the DataRelationsCollection Object. We can create parent child data relations between DataTable using Datarelation Object. We can relate one or more column from different tables using DataRelation Object . The columns involved in the DataRelation should be identical data types. That is the parent and child column should be similar Data Types

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

firstSql = "Your First SQL Statement Here"
secondSql = "Your Second SQL Statement Here"

You have to replace the string with your real time variables.

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim firstSql As String
Dim secondSql As String
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
firstSql = "Your First SQL Statement Here"
secondSql = "Your Second SQL Statement Here"
connection = New SqlConnection(connetionString)
Try
connection.Open()
command = New SqlCommand(firstSql, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "Table1")

adapter.SelectCommand.CommandText = secondSql
adapter.Fill(ds, "Table2")

adapter.Dispose()
command.Dispose()
connection.Close()
'creating data relations
Dim relation As DataRelation
Dim table1Column As DataColumn
Dim table2Column As DataColumn
'retrieve column
table1Column = ds.Tables("Table1").Columns(0)
table2Column = ds.Tables("table2").Columns(0)
'relating tables
relation = New DataRelation("relation", table1Column, table2Column)
'assign relation to dataset
ds.Relations.Add(relation)

MsgBox("Data relation completed")
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class

ADONET DATASET OLEDB

The DataSet contains the copy of the data we requested through the SQL statement. We can use Dataset in combination with OleDbDataAdapter class . The OleDbDataAdapter object allows us to populate Data Tables in a DataSet. We can use Fill method in the OleDbDataAdapter for populating data in a Dataset.

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"

sql = "Your SQL Statement Here"

You have to replace the string with your real time variables.

Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As OleDbConnection
Dim oledbAdapter As OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String
Dim i As Integer

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
sql = "Your SQL Statement Here"

connection = New OleDbConnection(connetionString)
Try
connection.Open()
oledbAdapter = New OleDbDataAdapter(sql, connection)
oledbAdapter.Fill(ds)
oledbAdapter.Dispose()
connection.Close()

For i = 0 To ds.Tables(0).Rows.Count - 1
MsgBox(ds.Tables(0).Rows(i).Item(0) & " -- " & ds.Tables(0).Rows(i).Item(1))
Next
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class

ADONET DATASET multiple tables SQLSERVER

The DataSet contains DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The SqlDataAdapter object allows us to populate DataTables in a DataSet. We can use Fill method in the SqlDataAdapter for populating data in a Dataset.

We can populate Dataset with more than one table at a time using SqlDataAdapter Object . The following VB.NET source code shows how to a single SqlDataAdapter fill Dataset with multiple tables.

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

sql = "Your SQL Statement Here"

You have to replace the string with your real time variables.


Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim i As Integer
Dim firstSql As String
Dim secondSql As String

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
firstSql = "Your First SQL Statement Here"
secondSql = "Your Second SQL Statement Here"
connection = New SqlConnection(connetionString)

Try
connection.Open()

command = New SqlCommand(firstSql, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "First Table")

adapter.SelectCommand.CommandText = secondSql
adapter.Fill(ds, "Second Table")

adapter.Dispose()
command.Dispose()
connection.Close()

'retrieve first table data
For i = 0 To ds.Tables(0).Rows.Count - 1
MsgBox(ds.Tables(0).Rows(i).Item(0) & " -- " & ds.Tables(0).Rows(i).Item(1))
Next
'retrieve second table data
For i = 0 To ds.Tables(1).Rows.Count - 1
MsgBox(ds.Tables(1).Rows(i).Item(0) & " -- " & ds.Tables(1).Rows(i).Item(1))
Next

Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class