Talk about DataSet

zhaozj2021-02-16  112

DataSet is an ADO.NET developer, which is developer, is a collection of data. It is a collection of DataReader defects. DataReader data processing is fast, but it is read-only, and once moved to the next line, You cannot view the data on the previous line, and Dataset can freely move the pointer. DataSet's data is disconnected from the database. DataSet can also be used in multi-layer applications, if the application runs in the business object of the intermediate layer to access the database, the business object is passed to the client application.

DataSet features: browsing, sorting, searching, filtering, processing grading data, cache changes, etc. It is also possible to interchange with XML data. DataSet can include multiple DATATABLE, you can save multiple query structures into a DataSet, easy to operate, and multiple DATAROW, DATACOLUMN can be viewed, and data can be viewed by these DataRow, Datacolumn, but need If the operation result is returned to the database, the DataAdapter's Update method can be called.

DataSet's operation:

DataSet DS

=

New

DataSet (); DataTable DT

=

New

DataTable

"

NewTable

"

DS.TABLES.ADD (DT);

DataSet DS

=

New

DataSet (); DataTable DT

=

DS.Tables.Add (

"

NewTable

"

);

The above two methods can add a DataTable in the DataSet to see if they need. After adding DataTable, you need to add rows and columns.

DataSet DS

=

New

DataSet (); DataTable DT

=

DS.Tables.Add (

"

NewTables

"

DataColumn col

=

Dt.column.add (

"

NewColumn

"

,

Typeof

(

int

); col.allowdbnull

=

False

COL.MAXLENGTH

=

4

COL.UNIQUE

=

True

;

The above code adds to the DataSet in the DataSet, named "newColumn", type Int and is not empty, the maximum length is 4 and the uniqueness of true columns.

Dt.primaryKey

=

New

Datacolumn []

{DT.COLUMNS ["ID"]}

This code continues above, add a primary key column to a DataTable, and the primary key column is a data set. If there are multiple primary keys, simply add a column to the array. as follows:

Dt.primaryKey

=

New

Datacolumns []

{DT.COLUMNS ["OrderID"], DT.COLUMNS ["ProductID"]}

Add foreign key:

ForeignKeyConstraint fk; fk

=

New

ForeignKeyConstraint (ds.tables

"

Customers

"

] .Columns [

"

Customerid

"

], DS.TABLES [

"

ORDERS

"

] .Columns [

"

Customerid

"

]); DS.TABLES ["

ORDERS

"

] .Constraints.add (fk);

//

The above code is if the primary key has been created for the Cusomers table and ORDERS, this sentence is adding foreign key constraints.

The above is to create constraints based on Customers tables and Orders tables.

Here are the contents of the modification of DataRow:

DataRow DR

=

DS.TABLES [

"

Customer

"

] .Rows.find

"

Anton

"

);

IF

(DR

==

NULL

)

Else

{Dr.BegineDit (); DR ["CompanyName"] = "newValue"; DR ["contactname"] = "newValue2"; Dr.Endedit ();

//

The above code is positioned through the row in the DataTable, find the "Anton" line, and then modify the value of the "Anton" line in the "Anton" line. Cache the modifications of the row via BegineDit and Endedit, you can also call Canceledit to cancel the modification.

Judging whether a column is empty:

DataRow DR

=

DS.TABLES [

"

Customers

"

] .Rows.find

"

AAA

"

);

IF

(Dr.isnull

"

ContactName

"

);

Else

DR [

"

ContactName

"

]

=

DBNULL.VALUE

//

It is judged here if the contactname column is empty. If it is not an empty value, huh, it is very unpredictable, here is only the practice of demonstrating a null value.

Delete DataOw:

There are two ways to delete the DataRow, the Delete method, and the REMOVE method and the REMOVEAT method. The difference is that the delete method is actually not removed from the DataTable, but the logo is deleted, just make a mark, and the REMOVE method is real from the DataRow, the REMOVEAT method is the index of the root line delete. Column:

DataRow DR

=

DS.TABLES [

"

TABLE

"

] .Rows.find

"

a

"

DS.TABLES [

"

TABLE

"

] .Remove (DR); or DS.TABLES [

"

TABLE

"

] .Remove (INDEX);

//

DR is the line where "a" is located, it is deleted after it is detected, index is the index number where "a" is located.

About the usage in DataSet, refer to MSDN

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

New Post(0)