วันจันทร์ที่ 17 มิถุนายน พ.ศ. 2556

การติต่อฐานข้อมูล

What is ADO.NET Dataset

The ADO.NET DataSet contains DataTableCollection and their DataRelationCollection . It represents a collection of data retrieved from the Data Source. We can use Dataset in combination with DataAdapter class. The DataSet object offers a disconnected data source architecture. The Dataset can work with the data it contain, without knowing the source of the data coming from. That is , the Dataset can work with a disconnected mode from its Data Source . It gives a better advantage over DataReader , because the DataReader is working only with the connection oriented Data Sources


The DataAdapter object allows us to populate DataTables in a DataSet. We can use Fill method of the DataAdapter for populating data in a Dataset. The DataSet can be filled either from a data source or dynamically. A DataSet can be saved to an XML file and then loaded back into memory very easily. The following links shows more information of Dataset in details.

How to Dataset with Sql Server

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.
VB.NET Source Code
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

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.

How to Dataset with OLEDB Data Source

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.
VB.NET Source Code
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

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.

Search Tables in a Dataset Sql Server

The DataSet contains DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The Dataset may comprise data for one or more members, corresponding to the number of rows. The SqlDataAdapter 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.
VB.NET Source Code
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 tables As DataTable
        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, "SQL Temp Table")
            adapter.Dispose()
            command.Dispose()
            connection.Close()
 
 
            For Each tables In ds.Tables
                MsgBox(tables.TableName)
            Next
 
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
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.

Search Tables in a Dataset OLEDB Data Source

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.
VB.NET Source Code
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
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

Dataset table row count in SQL Server

The DataSet contains copy of the data we requested through the SQL statement. The DataSet consists of DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The data set may comprise data for one or more members, corresponding to the number of rows. The following VB.NET source code shows how to find the number of rows in a table that resides in the Dataset.
VB.NET Source Code
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 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, "SQL Temp Table")
            adapter.Dispose()
            command.Dispose()
            connection.Close()
 
            MsgBox("Number of row(s)  -  " & ds.Tables(0).Rows.Count)
 
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
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.

Dataset table row count - OLEDB Data Source

The DataSet contains copy of the data we requested through the SQL statement. The DataSet consists of 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 following VB.NET source code shows how to find the number of rows in a table that resides in the Dataset from OLEDB Data Source.
VB.NET Source Code
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
 
        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()
 
            MsgBox("number of Row(s)   -   " & ds.Tables(0).Rows.Count)
 
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
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.

How to find column definition - Sql Server

Dataset represents a collection of data retrieved from the Data Source. The Dataset can work with the data it contains, without knowing the source of the data coming from. The SqlDataAdapter object allows us to populate DataTable in a DataSet. We can use Fill method in the SqlDataAdapter for populating data in a Dataset. The Dataset can contains more than one Table at a time. The data inside Table is in the form of Rows and Columns .
In some situations we have to find the column headers in a DataTable. There is a ColumnsCollection object in the Datatable hold the column Definitions.
VB.NET Source Code
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 dt As DataTable
        Dim column As DataColumn
        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, "SQL Temp Table")
            adapter.Dispose()
            command.Dispose()
            connection.Close()
 
            dt = ds.Tables(0)
            For Each column In dt.Columns
                MsgBox(column.ColumnName)
            Next
 
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
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.

How to find column definition - OLEDB Data Source

Dataset represents a collection of data retrieved from the Data Source.. The OleDbDataAdapter object allows us to populate DataTable in a DataSet. We can use Fill method in the OleDbDataAdapter for populating data in a Dataset. The Dataset can contain more than one Table at a time. The data set may comprise data for one or more members, corresponding to the number of rows.
In some situations we have to find the column headers in a DataTable. There is a ColumnsCollection object in the Datatable hold the column Definitions.
VB.NET Source Code
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 dt As DataTable
        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()
 
            dt = ds.Tables(0)
            For i = 0 To dt.Columns.Count - 1
                MsgBox(dt.Columns(i).ColumnName)
            Next
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
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.

How to create DataSet without Databse


Dataset represents a collection of data retrieved from the Data Source. The DataSet contains DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The data set may comprise data for one or more members, corresponding to the number of rows. We can fill the data in Dataset without calling SQL statements. For that we manually create a DataTable and add data in it.
VB.NET Source Code
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 ds As New DataSet
        Dim dt As DataTable
        Dim dr As DataRow
        Dim idCoulumn As DataColumn
        Dim nameCoulumn As DataColumn
        Dim i As Integer
 
        dt = New DataTable()
        idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
        nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))
 
        dt.Columns.Add(idCoulumn)
        dt.Columns.Add(nameCoulumn)
 
        dr = dt.NewRow()
        dr("ID") = 1
        dr("Name") = "Name1"
        dt.Rows.Add(dr)
 
        dr = dt.NewRow()
        dr("ID") = 2
        dr("Name") = "Name2"
        dt.Rows.Add(dr)
 
        ds.Tables.Add(dt)
 
        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 i
 
    End Sub
End Class
 

How to multiple tables in Dataset - Sql Server

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.
VB.NET Source Code
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
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

sql = "Your SQL Statement Here"
How to multiple tables in Dataset - OLEDB Data Source
The DataSet contains DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The OleDbDataAdapter object allows us to populate DataTables in a DataSet. We can use Fill method in the OleDbDataAdapter for populating data in a Dataset. We can populate Dataset with more than one table at a time using OleDbDataAdapter Object . The following VB.NET source code shows how to a single OleDbDataAdapter fill Dataset with multiple tables.
VB.NET Source Code
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 firstSql As String
        Dim secondSql As String
        Dim i As Integer
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        firstSql = "Your First SQL Statement Here"
        secondSql = "Your Second SQL Statement Here"
        connection = New OleDbConnection(connetionString)
        Try
            connection.Open()
            oledbAdapter = New OleDbDataAdapter(firstSql, connection)
            oledbAdapter.Fill(ds, "First Table")
            oledbAdapter.SelectCommand.CommandText = secondSql
            oledbAdapter.Fill(ds, "Second Table")
            oledbAdapter.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
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"

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

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

How to add relations between tables in a Dataset

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.
VB.NET Source Code
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
                               
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.

How to merge tables in a Dataset - Sql Server

The DataSet contains copy of the data we requested through the SQL statement. The SqlDataAdapter object allows us to populate DataTable 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 DataTableCollection contains zero or more DataTable objects.
In some situation we want to combine the result of multiple SQL query as a single result set. In that case we can use the Dataset's Merge method for doing this. The tables involved in the merge should be identical, that is the columns are similar data types .
VB.NET Source Code
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 dt As DataTable
        Dim firstSql As String
        Dim secondSql As String
        Dim i As Integer
                               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, "Table(0)")
            adapter.SelectCommand.CommandText = secondSql
            adapter.Fill(ds, "Table(1)")
            adapter.Dispose()
            command.Dispose()
            connection.Close()
 
            ds.Tables(0).Merge(ds.Tables(1))
            dt = ds.Tables(0)
 
            For i = 0 To dt.Rows.Count - 1
                MsgBox(dt.Rows(i).Item(0) & "  --  " & dt.Rows(i).Item(1))
            Next
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
                               

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.

How to merge tables in a Dataset - OLEDB Data Source

The DataSet contains copy of the data we requested through the SQL statement. The OleDbDataAdapter object allows us to populate DataTable in a DataSet. We can use Fill method in the OleDbDataAdapter for populating data in a Dataset. We can populate Dataset with more than one table at a time using OleDbDataAdapter Object. The DataTableCollection contains zero or more DataTable objects. In some situation we want to combine the result of multiple SQL query results as a single result set. In that case we can use the Dataset's Merge method for doing this. The tables involved in the merge should be identical. The following VB.NET source code shows how to merge the tables from two different dataset.
VB.NET Source Code
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 ds1 As New DataSet
        Dim ds2 As New DataSet
        Dim dt As DataTable
        Dim firstSql As String
        Dim secondSql As String
        Dim i As Integer
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        firstSql = "Your First SQL Statement Here"
        secondSql = "Your Second SQL Statement Here"
        connection = New OleDbConnection(connetionString)
        Try
            connection.Open()
            oledbAdapter = New OleDbDataAdapter(firstSql, connection)
            oledbAdapter.Fill(ds1, "First Table")
            oledbAdapter.SelectCommand.CommandText = secondSql
            oledbAdapter.Fill(ds2, "Second Table")
            oledbAdapter.Dispose()
            connection.Close()
 
            ds1.Tables(0).Merge(ds2.Tables(0))
            dt = ds1.Tables(0)
 
            For i = 0 To dt.Rows.Count - 1
                MsgBox(dt.Rows(i).Item(0) & "  --  " & dt.Rows(i).Item(1))
            Next
 
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"

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

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




ให้นักศึกษาสร้าง From Login ตามต้องการโดยตกแต่งให้สวยงาม จากนั้นให้ทำการเขียน Code เพื่อทำการเชื่อมต่อฐานข้อมูลโดยวันนี้จะใช้ Microsoft Access 2003 และ MS SQL Server 2005
·       สิ่งที่นักศึกษาจะได้ทำการเรียนรู้
1.               การ Import แบบ
             Imports System.Data.OleDb
             Imports System.Data.SqlClient
2.               การประกาศตัวแปรเพื่อทำการรับค่าต่างๆ
3.               การเขียนโปรแกรมแบบการใช้ Function เพื่อเรียกใช้
4.               และการกำหนด Properties ต่างๆให้กับเครื่องมือที่ใช้





4 ความคิดเห็น: