Executes the appropriate commands to delete, insert, or update the specified rows in the data source.
'Declaration
Public Function UpdateRows( _
ByVal () As DataRow _
) As Integer
Parameters
- datarows
- An array of System.Data.DataRow objects containing the rows to be updated.
Return Value
The number of rows successfully updated from the
DbDataTable.
The following code adds two records to a table and updates them. The table is rendered then to console.
static void UpdateATable(DbDataTable myDataTable, IDbConnection myConnection)
{
myDataTable.Connection = myConnection;
myDataTable.SelectCommand = myConnection.CreateCommand();
myDataTable.SelectCommand.CommandText = "SELECT EmpNo, EName FROM Test.Emp";
try
{
myDataTable.Active = true;
DataRow[] newRows = new DataRow[2];
newRows[0] = myDataTable.Rows.Add(new object[] {20000,"Jefferson"});
newRows[1] = myDataTable.Rows.Add(new object[] {20001,"Jameson"});
myDataTable.UpdateRows(newRows);
foreach(DataRow myRow in myDataTable.Rows)
{
foreach(DataColumn myCol in myDataTable.Columns)
{
Console.Write(myRow[myCol]+"\t");
}
Console.WriteLine();
}
}
finally
{
myDataTable.Active = false;
}
}
Public Sub UpdateATable(ByVal myDataTable As DbDataTable, ByVal myConnection As IDbConnection)
myDataTable.Connection = myConnection
myDataTable.SelectCommand = myConnection.CreateCommand()
myDataTable.SelectCommand.CommandText = "SELECT EmpNo, EName FROM Test.Emp"
Try
myDataTable.Active = True
Dim newVal1() As Object = {20000, "Jefferson"}
Dim newVal2() As Object = {20001, "Jameson"}
Dim newRows(1) As DataRow
newRows(0) = myDataTable.Rows.Add(newVal1)
newRows(1) = myDataTable.Rows.Add(newVal2)
myDataTable.UpdateRows(newRows)
Dim myRow As DataRow
Dim myCol As DataColumn
For Each myRow In myDataTable.Rows
For Each myCol In myDataTable.Columns
Console.Write(myRow(myCol) & Chr(9))
Next myCol
Console.WriteLine()
Next myRow
Finally
myDataTable.Active = False
End Try
End Sub