Beruflich Dokumente
Kultur Dokumente
ADO.Net
db2Connection = New
System.Data.OleDb.OleDbConnection("Provider=IBMDADB2.1;User
id=xxxxx;Password=yyyyy;Data Source=DSNP")
db2DataAdapter = New System.Data.OleDb.OleDbDataAdapter()
db2DataSet = New System.Data.DataSet()
End Sub
Try
db2Connection.Open()
db2Command = New
System.Data.OleDb.OleDbCommand(tbSQL.Text.ToString)
db2Command.Connection = db2Connection
db2DataAdapter.SelectCommand = db2Command
‘ execute the command and fill the DataTable with results
/conversion/tmp/scratch/43751030.doc
FIS Incorporated
ADO.Net
db2DataAdapter.Fill(table)
dgvDataGrid.DataSource = table
‘ alternatively could have filled dataset and then passed first table to
‘ datagrid
‘db2DataSet.Clear()
‘db2DataAdapter.Fill(db2DataSet)
‘dgvDataGrid.DataSource = db2DataSet.Tables(0)
Finally
db2Connection.Close()
End Try
End Sub
End Class
SQL commands and Much of this can be generated via the GUI tool
parameters
oleDbSelectCommand.Parameters.Add (New
System.Data.OleDb.DbParameter(“emplid”,
System.Data.OleDb.OleDbType.Char, 10,
System.Data.ParameterDirection.Input, False, CType(0,Byte),
Ctype(0,Byte), “emplid”, System.Data.DataRowVersion.Current, Nothing))
/conversion/tmp/scratch/43751030.doc
FIS Incorporated
ADO.Net
updateSQL.Add (New
System.Data.OleDb.DbParameter(“original_emplid”,
System.Data.OleDb.OleDbType.Char, 10,
System.Data.ParameterDirection.Input, False, CType(0,Byte),
Ctype(0,Byte), “emplid”, System.Data.DataRowVersion.Original, Nothing))
dataAdapter.UpdateCommand = updateSQL
Problem with a After a DataSet has been Filled, make sure to call DataSet.Clear before
DataSet? calling DataSet.Fill() again
Iterating through a Assuming DataSet.Fill 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.
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
System.Object, ByVal e As System.EventArgs) Handles
btnUpdate.Click
Dim row As System.Data.DataRow
Dim i As Integer
Try
dbUpdateCommand = New
System.Data.OleDb.OleDbCommand("update employees set
name = ? where emplid = ?")
dbConnection = getdbConnection()
dbConnection.Open()
dbUpdateCommand.Connection = dbConnection
For i = 0 To table.Rows.Count - 1
row = table.Rows(i)
If (row.RowState =
DataRowState.Modified) Then
dbUpdateCommand.Parameters.Add(New
System.Data.OleDb.OleDbParameter("name",
System.Data.OleDb.OleDbType.Char, 30))
dbUpdateCommand.Parameters.Add(New
/conversion/tmp/scratch/43751030.doc
FIS Incorporated
ADO.Net
System.Data.OleDb.OleDbParameter("emplid",
System.Data.OleDb.OleDbType.Char, 11))
dbUpdateCommand.Parameters.Item(0).Value =
row(1).ToString
dbUpdateCommand.Parameters.Item(1).Value =
row(0).ToString
dbUpdateCommand.ExecuteNonQuery()
End If
Next
Catch oleDbException As
System.Data.OleDb.OleDbException
MsgBox("Exception caught." &
oleDbException.ToString)
Finally
dbConnection.Close()
End Try
End Sub
Writing XML from a ‘ The following assumes the Dataset has already been filled.
DataSet
myDataSet.WriteXML(“xmlfilename.xml”)
‘ 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
dataSet.Relations.Add("EmpDeps", _
dataSet.Tables("Employees").Columns("EmplId"), _
dataSet.Tables("Dependents").Columns("EmplidID")).Nested = true
/conversion/tmp/scratch/43751030.doc