ADO.NET study notes (3)

xiaoxiao2021-03-06  41

(Since the code is written by C #, the converted to VB.Net is troubles, and will not be converted, forgive me). 5. Manipulating DataSet DataRow is the basic storage location of all the data, which is mainly A value array consists of DataTable separately. DataRow mainly includes several information: 1, the current value of each column in the line, 2, the original value of each column in the line, 3, row status, 4, the linked link between the father and the sub-line

Initialize a DATAROW: DATATABLE DATATABLE = Dataset.tables [0]; DATAROW NEWROW = DATATABLE.NEWROW (); // Generate DataRow with DataTable DataTable.Rows.Add (newrow);

Delete row: DataTable.Rows.remove; DataTable.Rows.Removeat (line number); DATAROW.DELETE (); // Row itself removal

Read and write the value of DataRow: Row ["column name"], ROW [列号] can reference one of the attributes Datacolumn a = dataable.columns ("column name"); / / You can get a column

Batch changes to the line: become -dit () start change, endedit () end changes, and write change results to DataSet, cancellation, for example: row.beginedit (); Change Row.Endedit () ;

Load the data volume to DataTableDataTable.BeginLoadData (); DataTable.loadDataRow (Row1, false); // When the second parameter is True, accept changes when calling DataTable.AcceptChanges (), adds a change directly to False ... DataTable.EndloadData ); Using this data load method can block all data constraints during data loading, and the index will not be maintained, which greatly speeds up the data loading speed.

Version: Current: Current value default: Default value according to the different lines of operations Original: The value after the last call accept: Call AcceptChanges () Changed the value to be changed, for example, to get the ORIGINAL value: String OldString = ROW ("Firstname", DataRowVersion.original);

The status of rows: row.rowstate gets the state of rows, such as deleting, becomes deleted, and data storage updates will become unchanged.

6. DataSet Navigation In addition to each table in ADO.NET, it maintains its relative independence, allowing the relevant lines between the different tables in the row (navigation to the sub-line, the father of the navigation) such as DataRow [] Invoiceerows = Custrown.getChildrows ("Customer_Invoice"); // Navigate through the relationship

Seven, DataViewDataView session time data view provides the database structure to provide external mode implementation. At the same time, DataView can also provide data binding functions for form controls and web controls, built a DataView in every DataTable: DataTable.defaultView ();

Create DataViewDataView SortedView = New DataView (DataTable); sort DataView.defaultView.Sort = "Lastname"; DataTable.defaultView.Sort = "Lastname, Firstname Desc";

Filter DataView: 1. Filter DATATABLE.DEFAULTVIEW.ROWFILTER = "Vendor = 'RAWLINGS'" by setting the RowFilter property settings, but the screening expression can only be set to relatively simple expressions, but the function is limited, but Meet the basic requirements. Similarly, you can also make a simple search, return a DATAROW array, an example: DataRow [] compoundrows = dataable.select ("Vendor = 'Wilson' and Price> 20.00) 2, filter DataTable.defaultView.rowStateFilter = "DataViewRowState.originalRows" can filter ROW that meets the requirements

Search for DataView: Filter with RowFilter with RowFilter with RowFilter, use Find, Findrows You must first set the DataView's sort property when searching with a row match with a specific key: int found = dataable .Defaultview.find ("Wilson"); // Get the location of the line DATAROWVIEW [] Rows = DataTable.defaultView.FindRows ("Rawlings") // Take a ROW array

8. Update DB In DataSet, each DataTable corresponds to a DataAdapter, DataAdapter.Update (), DataTable automatically updates. When updating, you can use CommandBuilder to automatically generate updated SQL commands based on DataSet's change, SQLCommandBuilder BLDR = New SQLCommandBuilder (DataAdapter); DataAdapter.Update (CustTable); but Update accepts DataSet parameters and does not update Dataset but update one of DataSet " Table "The table should take note of the time: 1. SelectCommand must be valid 2, must have a primary code 3, if the SELECTCOMMAND fills the DataTable process, it should call CommandBuilder.refreshschema ()" 4, updating DB is unreasonable, constrained or DataSet's impact although using CommandBuilder is more convenient, do not write update commands yourself, but automatically generated command performance is not high, you can consider writing stored procedures or use directly sql statement with parameters, for example: String insQry = "Insert into Customer (CustomerID) Values ​​(@Customer)"; SqlCommand insCmd = conn.CreateCommand (); insCmd.CommandText = insQry; SqlParameterCollection insParams = insCmd.Parameters; insParams.Add ( "@CustomerID", SqlDbType.UniqueIdentifier, 0, "CustomerID"); dataAdapter.InsertCommand = insCmd; dataAdapter.Update (); DataAdapter.Update at time of () updates the update range may be controlled: dataAdapter.Update (invTable. GetChanges (DataRowState.deleted); // only updated the deleted part of the nine, transaction tx = conn.begintransaction (isolationledLevel.Serializable); invda.selectcommand.transaction = TX; transaction TX.commit (); submit // tx. Rollback (); transaction rollback

Ten, data binding simple version: (for text box, label, etc.) {controls} .databindings.add ("{property}", {datasource}, "{datamember}); where Property is the properties to be bound, DataSource is DataView or DataTable, DataMember is a property in DataSource.

Complex version: (for ListBox, ComboBox, etc.) To set each property to implement binding: DataSource = A object (DataTable or DataView) Displaymember: A attribute in the DataSource to be displayed VALUEMEMEMBER: Determine which one is referenced in DataSource Data line, that is, the name value of Displaymember is implemented.

DataGrid binding: 1, you can set the DataSource property to implement static binding 2, you can use the setDatabase function to implement dynamic binding

At the same time, DataGrid supports the master / detailed table to display mastergrid.setdataBinding (Customertable, ""); DetailGrid.SetDataBinding (Customertable, "CUSTOMER_INVoices") // The second property is set to the relationship constraints such as a line in the primary table, in the child In the table, the corresponding line is found in the sub-table according to the middle and external code of the main table.

After the binding, there is a CurrencyManager property to implement the cursor function bindingContext [Customertable] Returns a CurrencyManager object, where the position property can be changed to implement the movement of the cursor.

转载请注明原文地址:https://www.9cbs.com/read-62901.html

New Post(0)