0% found this document useful (0 votes)
113 views4 pages

ADO.Net Data Access and Management Guide

The document discusses ADO.Net namespaces and classes for connecting to and manipulating data sources like databases. It provides an example of connecting to a DB2 database, executing a SQL query, and displaying results in a data grid. It also demonstrates how to iterate through and update a DataSet disconnected from the data source.

Uploaded by

jatin_mca2007
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views4 pages

ADO.Net Data Access and Management Guide

The document discusses ADO.Net namespaces and classes for connecting to and manipulating data sources like databases. It provides an example of connecting to a DB2 database, executing a SQL query, and displaying results in a data grid. It also demonstrates how to iterate through and update a DataSet disconnected from the data source.

Uploaded by

jatin_mca2007
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

FIS Incorporated

[Link]

Namespaces [Link] – contain classes to connect with and modify any


datasource (both databases and XML datasources)

[Link] - similar to OleDb above but optimized to work


with Microsoft SQL Sever databases

Classes [Link] - class that consists of a set of DataTables and


relationships among the DataTables. Mirrors the structure of a relational
databases but is disconnected from the database

[Link] – class that provides a


connection to a datasource

[Link] – a class that can populate a DataSet


class from a datasource

[Link] – Class containing an SQL


command to be executed on a datasource (this class does not cache
results in a DataSet). Each OleDbCommand must be given a
OleDbConnection to connect to the datasource
Example simple Connect to a DB2 datasource, execute the SQL entered in a text box and
connection and display the results to a DataGrid View.
retrieval

Public Class DB2Example


Friend WithEvents db2Connection As
[Link]
Friend WithEvents db2DataAdapter As
[Link]
Friend WithEvents db2Command As
[Link]
Friend WithEvents db2DataSet As [Link]
Private Sub Form1_Load(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]

db2Connection = New
[Link]("Provider=IBMDADB2.1;User
id=xxxxx;Password=yyyyy;Data Source=DSNP")
db2DataAdapter = New [Link]()
db2DataSet = New [Link]()

End Sub

Private Sub btnSubmit_Click(ByVal sender As [Link], ByVal e


As [Link]) Handles [Link]
Dim table As New DataTable

Try
[Link]()
db2Command = New
[Link]([Link])
[Link] = db2Connection
[Link] = db2Command
‘ execute the command and fill the DataTable with results

/conversion/tmp/scratch/[Link]
FIS Incorporated
[Link]

[Link](table)
[Link] = table

‘ alternatively could have filled dataset and then passed first table to
‘ datagrid
‘[Link]()
‘[Link](db2DataSet)
‘[Link] = [Link](0)

Catch oleDbException As [Link]


MsgBox("Exception caught.")

Finally
[Link]()

End Try

End Sub
End Class
SQL commands and Much of this can be generated via the GUI tool
parameters

A simple select statement

Friend WithEvents oleDbSelectCommand As


[Link]

oleDbSelectCommand = New [Link]()

[Link] = “Select emplname from


employees where emplid = ?”

[Link] (New
[Link](“emplid”,
[Link], 10,
[Link], False, CType(0,Byte),
Ctype(0,Byte), “emplid”, [Link], Nothing))

A simple update statement

Friend WithEvents updateSQL As [Link]

updateSQL = New [Link]()

[Link] = “Update employees Set emplid = ? ,


emplname = ? where emplid = ?”

[Link] (New [Link](“emplid”,


[Link], 10,
[Link], False, CType(0,Byte),
Ctype(0,Byte), “emplid”, [Link], Nothing))

[Link] (New [Link](“emplname”,


[Link], 30,
[Link], False, CType(0,Byte),

/conversion/tmp/scratch/[Link]
FIS Incorporated
[Link]

Ctype(0,Byte), “emplname”, [Link],


Nothing))

[Link] (New
[Link](“original_emplid”,
[Link], 10,
[Link], False, CType(0,Byte),
Ctype(0,Byte), “emplid”, [Link], Nothing))

[Link] = updateSQL

‘ ExecuteNonQuery for insert/update/delete


[Link]()

Problem with a After a DataSet has been Filled, make sure to call [Link] before
DataSet? calling [Link]() again
Iterating through a Assuming [Link] has been previously called.
DataSet Remember a DataSet looks and acts like a table(or set of tables,
relationships, etc.) but is disconnected from the original datasource. So
you can iterate through it to as you wish.

‘ Get the first table in the DataSet


Dim dataTable as DataTable = [Link](0)
Dim recordNumber As Integer

For recordNumber = 0 to [Link]


Try
‘ indexing is 0 based in [Link](rowNumber)(columnNumber)
Emplname = [Link]([Link](0)(0))
Next

Iterating through a A DataSet was filled from a datasource and the table in the DataSet was
DataTable and displayed in a DataGridView for viewing and updating. The following code
updating datasource updates the datasource when a change is found in the table
with changes
Private Sub btnUpdate_Click(ByVal sender As
[Link], ByVal e As [Link]) Handles
[Link]
Dim row As [Link]
Dim i As Integer
Try
dbUpdateCommand = New
[Link]("update employees set
name = ? where emplid = ?")
dbConnection = getdbConnection()
[Link]()
[Link] = dbConnection
For i = 0 To [Link] - 1
row = [Link](i)
If ([Link] =
[Link]) Then
[Link](New
[Link]("name",
[Link], 30))
[Link](New

/conversion/tmp/scratch/[Link]
FIS Incorporated
[Link]
[Link]("emplid",
[Link], 11))

[Link](0).Value =
row(1).ToString

[Link](1).Value =
row(0).ToString
[Link]()

End If

Next

Catch oleDbException As
[Link]
MsgBox("Exception caught." &
[Link])
Finally
[Link]()
End Try
End Sub
Writing XML from a ‘ The following assumes the Dataset has already been filled.
DataSet
[Link](“[Link]”)

‘ More often you would write the data using XSLT Transform
‘ Add dataset has been filled with results of 2 queries, emloyees and
dependents.
‘ The tables in the DataSet are joined by emplid
[Link]("EmpDeps", _
[Link]("Employees").Columns("EmplId"), _
[Link]("Dependents").Columns("EmplidID")).Nested = true

Dim xmlDataDocument As XmlDataDocument = New


XmlDataDocument(dataSet)
Dim xslTransform As XslTransform = New
[Link]("[Link]")

Dim xmlTextWriter As XmlTextWriter = New XmlTextWriter( _


"xslt_employee_dependents.html", [Link].UTF8)

[Link](xmlDataDocument, Nothing, writer)


[Link]()

/conversion/tmp/scratch/[Link]

You might also like