This example deletes a document from a data file using the CommandBuilder of an ADO.NET Data Adapter.
The code snippet sets the Select Command of a DataAdapter and calls its Fill method to load two columns of the MASTER table into a DataTable. All rows where [SERIAL NUMBER] < 10 are selected and deleted from the DataTable. The DataAdapter’s Update method is then called to commit these deletions. The CommandBuilder will automatically generate the Delete Command based on the adapter’s Select Command and deletions performed on the DataTable.
Assumptions:
The variable "cn" represents an open connection.
[INCLUDE NCSHEADER = TRUE] in the connection
PublicSub DeleteWithCommandBuilder(ByRef cn As _
System.Data.OleDb.OleDbConnection)
Dim myMasterTable AsNew System.Data.DataTable
'Create a command object that will return the serial number &
'document(number)columns of the master table
Dim cmdLoadRecords AsNew System.Data.Oledb.OleDbCommand
cmdLoadRecords.CommandText = _
"SELECT [SERIAL NUMBER], [DOCUMENT NUMBER] FROM MASTER"
cmdLoadRecords.CommandType = CommandType.Text
cmdLoadRecords.Connection = cn
Dim adpMaster AsNew System.Data.Oledb.OleDbDataAdapter
' Create an OleDbCommandBuilder and let it figure out
' what updates & deletes are required -
Dim cbDetails AsNew System.Data.OleDb.OleDbCommandBuilder(adpMaster)
' Setting the quote prefix and suffix is required
' because of field names containing spaces.
cbDetails.QuotePrefix = "["
cbDetails.QuoteSuffix = "]"
'Set the Select Command of the adapter
adpMaster.SelectCommand = cmdLoadRecords
'Populate a DataTable with the results of the Select Command
'This call would normally be enclosed in a Try/Catch.
adpMaster.Fill(myMasterTable)
'Select all documents whose serial number is < 10
Dim drs As System.Data.DataRow()
Dim dr As System.Data.DataRow
drs = myMasterTable.Select("[SERIAL NUMBER]< 10")
ForEach dr In drs
'Delete each row in the collection
dr.Delete()
Next
Console.WriteLine("No of rows deleted = " & _
myMasterTable.GetChanges(DataRowState.Deleted).Rows.Count())
'Call the adapter’s Update function
'This call would normally be enclosed in a Try/Catch.
adpMaster.Update(myMasterTable)
End Sub
See Help on Help for additional information on using this help file. See Scantron Technical Support for additional information on technical support and training options. See the ScanTools Suite System Requirements for further details on hardware and software requirements. ScanTools is a suite of products; the specific information you want may appear in the help for a different module. If you don't find what you're looking for here, try one of the following:
|
Scantron Corporation
Customer Service (forms, products, and services): 1-800-SCANTRON (722-6876) Technical Support: 1-800-445-3141 |
|
Copyright © 1998-2012 Scantron Corporation. All rights reserved. Use permitted only under license. www.scantron.com. No part of the Help or user guides may be reproduced in any form, or by any means, without express permission from Scantron Corporation. LINKS TO THIRD PARTY SITES This help system may contain links to third party websites ("Linked Sites"). The Linked Sites are not under the control of Scantron and Scantron is not responsible for the content of any Linked Site, including without limitation any link contained in a Linked Site or any changes or modifications to a Linked Site. Scantron is not responsible for web casting or any other form of transmission received from any Linked Site. Scantron provides Users with the ability to link the Assessment System to the Linked Sites as a convenience to you, and the inclusion of any link does not imply endorsement by Scantron of the Linked Site or any association with its operators. |