Sie sind auf Seite 1von 71

Introduction to ADO.NET 2.

0
Agenda

† Plain Talk
† .NET Framework Data Provider
† Operation without Connection to a Database
† Applications
pp cat o s
Plain Talk
Categories of Objects

† Connected Objects:
„ Connecting a database
„ Read-onlyy record set
„ Forward-only stream

† Disconnected Objects:
„ No connection to a database
„ Off-line operation
„ Sorting, Searching, Filtering, Modifying
ADO.NET Architecture

.NET Framework DataSet


Data Provider
DataTableCollection
Connection
DataAdapter
Transaction
DataTable
SelectCommand
D t R C ll ti
DataRowCollection
InsertCommand
Command
DataColumnCollection

Parameters UpdateCommand
ConstraintCollection

DeleteCommand

DataReader
DataRelationCollection

DB XML
Connected Objects –
C
Connection
ti Obj
Objectt
† A Connection for communicating with a
Database
† As an ADO
O Connection Object
O
Connected Objects –
C
Commandd Obj
Objectt
† Query
† Execute a Stored Procedure
† Any
A SQL Command
C d
† As an ADO Command Object
Connected Objects –
D t R d Obj
DataReader Objectt
† Data Row is Read-Only
Read Only
† Keep one row only
† Forward-only
F d l stream
t
Connected Objects –
D t Ad t Obj
DataAdapter Objectt
† A Bridge for Disconnected Objects
† One DataAdapter mapping to one DataTable
Disconnected Objects –
D t T bl Object
DataTable Obj t
† Retrieving data from the Rows property
† As an ADO Recordset Object
Disconnected Objects –
D t S t Obj
DataSet Objectt
† A container for DataTable objects
† Exchange easily with XML Docs.
† As
A an ADO Recordset
R d t Obj
Objectt
Disconnected Objects –
D t R l ti Obj
DataRelation Objectt
† Define a relation between DataTable objects
† i.e. Matster-to-Detail
† Define
D fi a rule.l i.e.
i Delete
D l t th
the d
details
t il when
h tto
delete the master"s record.
Disconnected Objects –
D t Vi
DataView Object
Obj t
† Define a rule.
rule ii.e.
e Sorting or Filtering a
DataTable
.NET
NET Framework Data Provider
Using the Connection Object (one)

† Using
g a Pooling
g Connection by
y default
( SQL Server)
† Using a connection: ( VB + SQL Server )
„ Dim myConnection as SqlConnection
„ myConnection.ConnectionString = “…”
„ myConnection.Open()
„ myConnection.Close
y ()

† Create a Transaction Object synchronously


( VB+ SQL Server )
„ Dim mySqlTransaction as SqlTransaction
„ mySqlTransaction = myConnections.BeginTransaction
„ mySqlTransaction.Commit ()
„ mySqlTransaction.Rollback ()
Using the Connection Object (two)

† Create a Command Object synchronously


„ Dim myCommand as SqlCommand
„ myCommand = myConnection.CreateCommand

† Other usages:
„ Property: State
„ Methods: Open, Close
„ Event: StateChange
Using the Command Object (one)

† How to get a Command Object?


† Operation without a result set.
† Operation with a result set
set.
† Operation with Parameters.
† Operation with a Stored Procedure with
Parameters.
† Operation with a Stroed Procedure with a return
value.
Using the Command Object (two)

† How to get a Command Object?


( VB + SQL Server )
„ A) myCommand = myConnection.CreateCommand
† myCommand.CommandText = “…”

„ B)) myCommand
y = SqlCommand.Create
q ()
† myCommand.Connection = myConnection
† myCommand.CommandText = “…”

„ C) myCommand = SqlCommand.Create(mySqlStr, myConnection)


Using the Command Object (three)

† Operation without a result set:


„ DML (Data Manipulation Language)
† Insert,
Insert Update,
Update Delete
„ DDL (Data Definition Language)
† Create Table
Table, Alter View
View, Drop Table
Table, and so on
on.

How to do?
† myCommand.CommandText = “Update permis set…”
† myCommand.ExecuteNonQuery ()
Using the Command Object (four)

† Operation with a result set


„ DataReader Object:
† Call the ExecuteReader method of the Command object, rather
th di
than directly
tl using
i a constructor
t t
† Don"t open another DataReader object in the same connection
† Forward-only stream
† Read-only result set
† Keep one row only

How to do?
„ Dim myDataReader as SqlDataReader
„ myCommand.CommandText = “select * from PermisUser"”
„ myDataReader = myCommand.ExecuteReader
„ While myDataReader.Read()
„ Response.Write (myDataReader(0), myDataReader(1))
„ End While
„ reader.Close()
Using the Command Object (five)

„ Read a field of data in the DataReader


† Dim myDataReader as SqlDataReader
† myCommand.CommandText = "select emp_na from PermisUser"
† myDataReader = myCommand.ExecuteReader()
† myDataReader.Read()
† strEmpName
p = myDataReader.Item(
y ( "emp
p_na" )).ToString()
g()

„ Special operation:
† Dim sCount as String
† myCommand.CommandText = "select count(*) from PermisUser”
† sCount = myCommand.ExecuteScalar.ToString()
myCommand ExecuteScalar ToString()
Using the Command Object (six)

† Operation with Parameters ( VB + SQL Server )


„ myCommand.CommandText =
"select emp
p_na from CtpPermisUser
p where emp
p_no =
@Param1”
„ myCommand.Parameters.Add("@Param1", SqlDbType.VarChar)
„ myCommand Parameters(0) Value = "92110015”
myCommand.Parameters(0).Value 92110015
„ myDataReader = myCommand.ExecuteReader()
„ myDataReader.Read()
„ sResult = myDataReader.Item("emp_na").ToString()

† SQL Server .NET Provider supports Parameters with the “@ + ParamName” format.
Using the Command Object (seven)

† Operation with a Stored Procedure with


Parameters ( VB + SQL Server )
„ myCommand.CommandText
C dC dT t = "spGetCM”
" G tCM”
„ myCommand.CommandType = CommandType.StoredProcedure
„ myCommand.Parameters.Add("@PlayDate", SqlDbType.VarChar)
„ myCommand Parameters Add("@PlayDate2" SqlDbType
myCommand.Parameters.Add("@PlayDate2", SqlDbType.VarChar)
VarChar)
„ myCommand.Parameters("@PlayDate").Value = "2004/01/12"
„ myCommand.Parameters("@PlayDate2").Value = "2004/01/13"
„ myDataReader = myCommand
myCommand.ExecuteReader()
ExecuteReader()
„ myDataReader.Read()
„ sResult = myDataReader.Item("CM_Na").ToString()

† CommandType: Text (default), StoredProcedure, TableDirect


Using the Command Object (eight)

† Operation with a Stored Procedure with a return


value ( VB + SQL Server )
„ myCommand.CommandText = "spGetCM"
„ myCommand.CommandType = CommandType.StoredProcedure
„ myCommand.Parameters.Add("@Return_Value", SqlDbType.Int)
„ myCommand.Parameters.Add("@PlayDate",
C dP t Add("@Pl D t " SqlDbType.VarChar)
S lDbT V Ch )
„ myCommand.Parameters.Add("@PlayDate2", SqlDbType.VarChar)
„ myCommand.Parameters.Direction = ParameterDirection.RetuenValue
„ myCommand.Parameters("@PlayDate").Value
myCommand.Parameters( @PlayDate ).Value = "2004/01/12"
2004/01/12
„ myCommand.Parameters("@PlayDate2").Value = "2004/01/13"
„ myCommand.ExecuteNonQuery()
„ strResult = myCommand.Parameters("@Return_Value").Value.ToString()

† Direction: Input (default), Output, InputOutput, ReturnValue


Using the DataAdapter Object (one)

† A bridge between Connected Objects and


Disconnected Objects. (DataSet, DataTable)

DataAdapter

DataSet
Database DataTable
Using the DataAdapter Object (two)

† How to get a DataAdapter Object?


( VB + SQL Server )

Dim myDataAdapter
Di D Ad as SqlDataAdapter
S lD Ad
„ A) myDataAdapter = SqlDataAdapter.Create(strSQLSelect, strConnection)
„ This is not a good idea. It will establish another connection.

„ B) myDataAdapter = SqlDataAdapter.Create(strSQLSelect, myConnection)

myCommand
m Command = SqlCommand.Create(strSQLSelect,
SqlCommand C eate(st SQLSelect myConnection)
m Connection)
„ C) myDataAdapter = SqlDataAdapter.Create()
„ myDataAdapter.SelectCommand = myCommand

„ D) myDataAdapter = SqlDataAdapter.Create(myCommand)
Using
g the DataAdapter Object
j (three)

† Move a result set from a DataAdapter to DataSet:


Fill method.
„ Auto open an established connection which had be
closed.
„ Auto create a DataTable object in the DataSet.
„ By default, the DataTable object name is Table.
„ Fill with a result set with ColumnName,
ColumnName Ordinal,
Ordinal and
DataType.
„ Paging
g g throughg aq query y result.
„ Move a result set from ADO to ADO.NET
Using the DataAdapter Object (four)

† How to use the Fill? ( VB + SQL Server )


Dim myDataSet as DataSet
myDataSet = DataSet.Create()
myDataAdapter = SqlDataAdapter.Create(strSQLSelect, myConnection)
„ A) myDataAdapter.Fill(myDataSet)
myDataAdapter Fill(myDataSet)
„ B) myDataAdapter.TableMappings.Add("Table", "Customers")

„ myDataAdapter.Fill(myDataSet)
„ C) myDataAdapter.Fill(myDataSet, "myTableName")

„ D) myDataAdapter.Fill(myDataSet, nStartRow, nRows, "myTableName")


„ nStartRow is zero-base.

„ E) myDataAdapter.Fill(myDataTable)
„ If you have a DataTable object
Using the DataAdapter Object (five)

† Applications:
„ You will fill the same DataTable twice:
† If your DataTable has no primary key:
„ The result set from the DataAdapter is appended.
† If your DataTabel has a primary key:
„ DataTable is updated by the primary key
key.
„ Use the FillSchema method to let your DataTable get a primary
key as its mapping table in our database.
† myDataAdapter FillSchema(myDataSet SchemaType
myDataAdapter.FillSchema(myDataSet, SchemaType.Source)
Source)

„ Avoid that result set is duplicate in the DataTable.


Fi t to
First t clear
l the
th DataTable,
D t T bl thenth fill DDataTable
t T bl with
ith a
new result set.
Using the DataAdapter Object (six)

† Other usages:
Methods:
„ Fill

„ FillSchema

„ Update

Events:
„ FillError

„ RowUpdating

„ RowUpdated
R U d t d
Operation without Connection to a
Database
Using the DataSet Object (one)

† You can:
„ scroll, search, sort, and filter a DataSet object.
„ define a master-detail relation
relation.
„ modify the DataSet of content.
„ integrated with XML DocDoc.
Using the DataSet Object (two)

† How to get a DataSet object? ( VB + SQL Server )

Dim myDataSet as DataSet


„ A) myDataSet = DataSet.Create()

„ B) myDataSet = DataSet.Create("myDataSetName")
Using the DataSet Object (three)

† Use the DataRow object


„ Read the 1st row, the 2nd column which name is
Emp Na ( VB + SQL Server )
Emp_Na.

myDataAdapter.Fill(myDataSet, "myTableName")

„ A) myDataSet.Tables(0).Rows(0)(1).ToString
„ B) myDataSet.Tables("myTableName").Rows(0)("Emp_Na").ToString
„ C) myDataSet.Tables.Item(0).Rows.Item(0).Item(1).ToString
„ D) myDataSet.Tables.Item("myTableName").Rows.Item(0).Item("Emp_Na").ToString
Using the DataSet Object (four)

† Use the DataColumn object


„ Read properties of the 2nd column which column
Emp Na. ( VB + SQL Server )
name is Emp_Na.

myDataAdapter.Fill(myDataSet, "myTableName")

„ A) myDataSet.Tables("myTableName").Columns(1).ColumnName.ToString
„ B) myDataSet.Tables(
myDataSet.Tables("myTableName").Columns("Emp
myTableName ).Columns( Emp_NaNa").Ordinal.ToString
).Ordinal.ToString
„ C) myDataSet.Tables("myTableName").Columns("Emp_Na").DataType.ToString
Using the DataSet Object (five)

† Other usages
„ Methods:
† AcceptChanges † GetXml
† RejectChanges † GetXmlSchema
† GetChanges † ReadXml
† Clear † ReadXmlSchema
† Clone † WriteXml
† Copy
Cop † WriteXmlSchema
† Merge

† Reset
Using the DataTable Object (one)

† One DataTable exists in one DataSet only.

get it? ( VB + SQL Server )


How to g
„ A) myAdapter.Fill(myDataSet, "TableName")
„ B) myAdapter.FillSchema(myDataSet, SchemaType.Source,
"TableName"))
„ C) Dim myDataTable as DataTable
Dim myColumn as DataColumn
myDataTable = myDataSet.Tables.Add(
myDataSet.Tables.Add("TableName")
TableName )
myColumn = myDataTable.Add("Emp_No")
myColumn.MaxLength = 10
„ Create a DataTable with a string type of Emp_No
Emp No field.
myColumn = myDataTable.Add("HowOld", TypeOf(Integer))
Using the DataTable Object (two)

† Add Constraints: ( VB + SQL Server )


„ AllowDBNull
† myColumn.AllowDBNull
myColumn AllowDBNull = False
„ ReadOnly
† myColumn.ReadOnly
myColumn ReadOnly = True
„ Unique
† myColumn.Unique
myColumn Unique = True
„ Column of MaxLength
† myColumn.MaxLength
myColumn MaxLength = 5
Using the DataTable Object (three)

„ Primary
a y Key
ey Co
Constraint?
st a t
† Combine two fields for a Primary Key. ( VB + SQL Server )

„ Dim aryColumn as DataColum()

„ aryColumn(0) = myDataTable.Columns.Add(
myDataTable Columns Add("Emp
Emp_No
No",
TypeOf(Integer))
„ aryColumn(1) = myDataTable.Columns.Add("Emp_Name")
„ myDataTable.PrimaryKey
D t T bl P i K = aryColumn
C l
Using the DataTable Object (four)

„ Foreign Key Constraint?


† Use the ConstraintCollection. ( VB + SQL Server )

„ Dim myForeignKey as ForeignKeyConstraint


„ Dim myMasterCol,
y , myDetailCol
y as DataColumn

„ myForeignKey = ForeignKeyConstraint.Create(myMasterCol,
myDetailCol)
„ myDataTable.Constraints.Add(myForeignKey)
Using the DataTable Object (five)

† Can I add a calculated field? ( VB + SQL Server )

„ Dim strSubtotal as String

„ strSubtotal = "AmountFieldName * CostFieldName"


„ myDataTable.Columns.Add("myCalcFieldName", TypeOf(Integer),
strSubtotal)

† Any functions can be put inside the strSubtotal.


Using the DataTable Object (six)

† Insert:
se t
„ If your DataTable is… ( VB + SQL Server )

Emp No
Emp_No Emp Name
Emp_Name Emp Birth
Emp_Birth Emp Salary
Emp_Salary
92010001 Andy 1965/01/21 32000
92010002 Bill 1970/08/04 29000

† Dim myRow as DataRow // insertion


† myRow = myDataTable
myDataTable.NewRow()
NewRow()
† myRow("Emp_No") = "92090020"
† myRow("Emp_Name")
y ( p ) = "Tom"
† myDataTable.Rows.Add(myRow) // don"t forget
† Maybe you use the myDataTable.LoadDataRow(…)
Using the DataTable Object (seven)

† Edit:
Dim theRow as DataRow
„ theRow = myDataTable.Rows.Find("92010002")
„ If theRow Is Nothing then
„ ShowMessage("Primary-Key Value not found.")
„ Else
„ theRow("Emp_Salary") = 33000
or
„ …
„ Else begin
Reference
„ theRow.BeginEdit
Method:
„ theRow("Emp
theRow( Emp_Salary
Salary")) = 33000 CancelEdit()

„ theRow.EndEdit Events:

End
RowChanging
„ RowChanged
ColumnChanging
C l
ColumnChanged
Ch d
Using the DataTable Object (eight)

† Delete:
† Use the Delete() to mark.
† Use
U th the Remove()
R () / RemoveAt()
R At() to
t remove the
th row
truly.
† Use the Clear() to remove all rows
rows.

† theRow Delete()
theRow.Delete()
or
† myDataTable.Remove(theRow)
or
† myDataTable.Clear()
Using the DataTable Object (nine)

† Verify the state of the row – RowState property


„ myDataSet.Tables("CtpPermisUser").Rows(nRowIndex).RowState

Constant Value Desc.


Detached 1 The row has been created but is not part of any
DataRowCollection.. A DataRow is in this state
DataRowCollection
immediately after it has been created and before it is
added to a collection, or if it has been removed from a
collection.

Unchanged 2 The row has not changed since AcceptChanges was


last called.

Added 4 The row has been added to a DataRowCollection


DataRowCollection,, and
AcceptChanges has not been called.

D l t d
Deleted 8 The row was deleted using the Delete method of the
DataRow..
DataRow

Modified 16 The row has been modified and AcceptChanges has


not been called.
Using the DataTable Object (ten)

† DataRowVersion Enumeration
Enumeration. ( VB+ SQL Server )

† sResult =
myDataTable.Rows("Emp_Name",
DataRowVersion.Current).ToString
Using the DataTable Object (eleven)

† Other usages
„ Methods:
† AcceptChanges
p g † BeginLoadData
g
† RejectChanges † EndLoadData

† GetChanges † ImportRow

† Clear

† Clone

† Copy

† Compute

† Reset
DataColumn Objects

† Other usages
„ Properties:
† Caption

† DefaultValue

† Table
DataRow Objects

† Other usages
„ Properties: „ Methods:
† RowError † AcceptChanges
† Table † RejectChanges

† CancelEdit

† GetChildRows

† GetParentRow

† GetParentRows
GetParentRo s
† SetParentRow

† IsNull
Use the DataRelation Object (one)

† Make a relation between DataTable-s.


DataTable s.
„ How to get it?
Dim myRelation as DataRelation
Dim myCustTable, myOrderTable as DataTable

† myRelation = DataRelation.Create(
DataRelation Create("RelationName"
RelationName ,

myCustTable.Columns("CustID"),

myOrderTable.Columns("CustID"))
† myDataSet.Relations.Add(myRelation)

† Maybe the 2nd & 3rd parameters are the "array of DataColumn".
Use the DataRelation Object (two)

† Get the Details:


Dim theCustRow, theOrderRow as DataRow

„ For theOrderRow in theCustRow.GetChildRows("RelationName") do


ShowMessage(theOrderRow("ProdName").ToString)

Get the Master from one Detail?


„ theCustRow = theOrderRow.GetParentRow("RelationName")
( )
„ ShowMessage(theCustRow("CustName").ToString)

† H
How about
b t th
the G
GetParentRows()?
tP tR ()?
Use the DataRelation Object (three)

† If you establish a DataRelation object


object, …
„ UniqueConstraint is added.
„ ForeignKeyConstraint is added
added.
„ Exist constraints are added.
Use the DataRelation Object (four)

† Other usages
„ Properties:
† ChildTable

† ParentTable

† Nested
Applications
Searching, Sorting, Filtering (one)

† Searching: Find()
„ Find a row by the Primary Key.

The Primary Key included only one column.


† theRow = myDataTable.Rows.Find("myProdId")

or, the primary key is established by two columns.


† Dim aryKey as object()

† aryKey(0) = nSerialNo

† aryKey(1) = "myProdId"

† theRow = myDataTable.Rows.Find(aryKey)
Searching, Sorting, Filtering (two)

† Searching: Select()
„ Find rows with conditions.
Dim theRows as DataRow()
† Dim strCondition as String
† strCondition = "ProdType = ""PO"" and Cost > 10000 and ID like
""A%"""
† theRows = myDataTable.Select(strCondition)

† S ti
Sorting: S l t()
Select()
† Var strSort : String
† strSort = "Cost Desc"
† theRows = myDataTable.Select(strCondition, strSort)
Searching, Sorting, Filtering (three)

† Searching: Select() with DataViewRowState


„ Searching changed rows inside the DataTable.

Dim enuDvrs as DataViewRowState


† enuDvrs = DataViewRowState.Added or DataViewRowState.Deleted

† theRows = myDataTable.Select("", "", enuDvrs)


Searching, Sorting, Filtering (four)

DataView objects?
† Not a SQL command.
† One DataView is mapping to one DataTable
DataTable.
† A DataTable must have a TableName.
† Including all columns from one DataTable.
† Searching,
g, sorting,
g, filtering,
g, editing,
g, and
navigation.
Searching, Sorting, Filtering (five)

† Get a DataView object:

Dim myDataTable as DataTable


Dim myDataView as DataView

„ myDataTable
D t T bl = myDataAdapter.Fill(myDataSet,
D t Ad t Fill( D t S t ""myTableName")
T bl N ")
„ myDataView = DataView.Create(myDataTable)
Searching, Sorting, Filtering (six)

† Retrieve rows from a DataView object:


j

Dim enuDvrs as DataViewRowState


„ enuDvrs
D =DDataViewRowState.Added
Vi R S Add d or DataViewRowState.Deleted
D Vi R S D l d
„ myDataView = DataView.Create(myDataTable,
"ProdNo like ""S%"" ",
Cost Desc
"Cost Desc",
enuDvrs)
or
„ myDataView = DataView.Create
DataView Create
„ myDataView.Table = myDataTable
„ myDataView.RowFilter = "ProdNo like ""S%"""
„ myDataView.Sort
y = "Cost Desc"
„ myDataView.RowStateFilter = enuDvrs
Searching, Sorting, Filtering (seven)

† Browsing a row in a DataView:


„ Use the Count property.
Dim theRowView : DataRowView
† For nRowIndex = 0 to myDataView.Count - 1
theRowView = myDataView(nRowIndex)
ShowMessage(theRowView("theFieldName").ToString)
Next
or
† For nRowIndex = 0 to myDataView.Count - 1
sResult = myDataView(nRowIndex)("theFieldName")
myDataView(nRowIndex)( theFieldName ).ToString
ToString
ShowMessage(sResult)
Next

„ Use the GetEnumerator() method.


Searching Sorting,
Searching, Sorting Filtering (eight)
† Find() in the DataView object
object.
DataView Object DataTable.Rows
Searching Condition By Sorting Fields By Primary Key Fields
Found Row Index DataRow object
Not Found -1 Nil Null
Nil,

† FindRows() ?
„ Return an array of DataRowView objects.
† Dim aryRows as Object()
† aryRows = myDataView.FindRows(mySearchingValue)
myDataView FindRows(mySearchingValue)
† If ( Length(aryRows) > 0 ) then …
Editing with DataRowView

† Add a new row.


row
Dim theRowView as DataRowView
„ theRowView = myDataView.AddNew()
„ theRowView("ProdName") = "xxx"
„ theRowView.EndEdit() // update the DataTable object

† M dif a row.
Modify
„ theRowView.BeginEdit()
„ theRowView("ProdName") = "yyy"
„ theRowView.EndEdit()

† Delete a row.
„ theRowView.Delete()
DataView & DataRowView Objeccts

† Other usages:
„ DataView objects
† Methods: CopyTo,
CopyTo Delete
† Properties: AllowDelete, AllowEdit, AllowNew,
RowStateFilter

„ DataRowView objects
† Methods: CancelEdit, CreateChildView

† Properties: IsEdit,
IsEdit IsNew
IsNew, Item
From DataSet to Database (one)

† Intermediate: SqlDataAdapter Objects

† Requirement:
„ SqlDataAdapter.InsertCommand
„ S lD t Ad t D l t C
SqlDataAdapter.DeleteCommandd
„ SqlDataAdapter.UpdateCommand
„ SqlCommand.Connection
„ SqlCommand.Parameters
From DataSet to Database (two)

† InsertCommand:
„ Initial
strSQL = "insert into CustTable(CustNo) values (@CustNo)"
„ myDataAdapter.InsertCommand.Connection
D t Ad t I tC dC ti = mySqlConnection
S lC ti
„ myDataAdapter.InsertCommand.CommandText = strSQL

„ myDataAdapter.InsertCommand.Parameters.Add("@CustNo",

SqlDbType.VarChar)

„ E
Execute
t
Dim theRow as DataRow
„ myDataAdapter.InsertCommand.Parameters("@CustNo").Value
y p ( @ ) =

theRow("CustNo").ToString()
„ myDataAdapter.InsertCommand.ExecuteNonQuery()
From DataSet to Database (three)

† DeleteCommand:
„ Initial
strSQL = "delete from CustTable where CustNo = @CustNo"
„ myDataAdapter.DeleteCommand.Connection = mySqlConnection
„ myDataAdapter.DeleteCommand.CommandText = strSQL
„ myDataAdapter.DeleteCommand.Parameters.Add("@CustNo",
SqlDbType.VarChar)

„ Execute
Dim theRow as DataRow
„ myDataAdapter.DeleteCommand.Parameters("@CustNo").Value =
theRow( CustNo ,
theRow("CustNo",
DataRowVersion.Original).ToString()
„ myDataAdapter.DeleteCommand.ExecuteNonQuery()
From DataSet to Database (four)

† UpdateCommand:
strSQL = "update CustTable set CustType = @CustType_New where CustType =
@CustType"
„ myDataAdapter.UpdateCommand.Connection
y p p = mySqlConnection
y q
„ myDataAdapter.UpdateCommand.CommandText = strSQL
„ myDataAdapter.UpdateCommand.Parameters.Add("@CustType_New",
SqlDbType.VarChar)
Sq b ype a C a )
„ myDataAdapter.UpdateCommand.Parameters.Add("@CustType",
SqlDbType.VarChar)

„ E ec te
Execute
Dim theRow as DataRow
„ myDataAdapter.UpdateCommand.Parameters("@CustType_New").Value =
theRow( CustType ,
theRow("CustType",
DataRowVersion.Current).ToString()
„ myDataAdapter.UpdateCommand.Parameters("@CustType").Value =
theRow("CustType",
DataRowVersion.Original).ToString()
g ) g()
„ myDataAdapter.UpdateCommand.ExecuteNonQuery()
From DataSet to Database (five)

† Updated the Database


Database. Client?
„ Only AcceptChanges()
XML in ADO.NET

† strXML = myDataSet.GetXml()
myDataSet GetXml()
„ The strXML is a string list table by table.

† myDataSet.WriteXML(“C:\myXMLFile.xml”,
D t S t W it XML(“C \ XMLFil l” XMLWriteMode.WriteSchema)
XMLW it M d W it S h )
End & Thank you.
y
Author : Benjamin
Editor : Albert

Das könnte Ihnen auch gefallen