ADO.NET Reading Notes Series ------ DataSet object

xiaoxiao2021-03-06  70

I. Features

1. Handle offline data and useful in multi-layer applications.

2, you can view the contents of the DataSet at any time, allowing the method of modifying the query results.

3, process grading data

4, cache change

5, XML integrity: DataSet objects and XML documents are almost interchangeable.

Second, use introduction

1. Create a DataSet object: DataSet DS = New Dataset ("DatasetName");

2, check the structure created by calling SqlDataAdapter.Fill

Da.fill (DS, "Orders");

DataTable TBL = DS.TABLE [0];

Foreach (Datacolumn COL in Tbl.columns)

Console.writeline (col.columnname);

3, check the data returned by SqlDataAdapter

1, DataRow object

DataTable TBL = DS.TABLE [0];

DATAROW ROW = TBL.ROW [0];

Console.WriteLine (ROS ["OrderID"]);

2, check the data stored in DATAROW

DataTable tbl = row.table;

Foreach (Datacolumn COL in Tbl.columns)

Console.writeline (row [col]);

3, check the DataRow object in dattable

Foreach (DataRow Row In Tbl.Rows)

DisplayRow (ROW);

4, check data in dataset

1, verify the properties of Datacolumn: Readonly, AllowDBnull, Maxlength, Unique

2, the constrains collection of DataTable objects: UiQueConstraints, PrimaryKey, ForeignKeyConstraints

You usually do not have to deliberately create ForeignKeyConstraints because one is created when you create a relationship between the two DataTable objects of the DataSet.

3, use SqlDataAdapter.Fill mode to retrieve mode information

5, write code to create a DataTable object

1, create a DataTable object: DataTable TBL = New DataTable ("TableName");

2, add DataTable to the table collection of the DataSet object

DataSet DS = New Dataset ();

DataTable TBL = New DataTable ("Customers");

Ds.Tables.Add (TBL);

DataSet DS = New Dataset ();

DataTable TBL = DS.TABLES.ADD ("Customers");

The DataTable object can only exist in the up to one DataSet object. If you want to add DataTable to multiple DataSets, you must use a COPY method or a Clone method. Copy methods create a new DataTable with the original DATATABLE structure and contain the same row; Clone method is the same as the original DATATABLE structure, but does not contain any new DataTable.

3, add columns for DataTable

DataTable TBL = DS.TABLES.ADD ("Orders");

Datacolumn col = tbl.columns.add ("ORDERID", TypeOf (int));

Col.allowdbnull = false;

COL.MAXLENGTH = 5;

Col.unique = true; tbl.primarykey = new datacolumn [] {tbl.columns ["customersid"]};

When the primary key is set, the allowdbnull is automatically set to false;

4, process automatic incrementum

DataSet DS = New Dataset ();

DataTable TBL = DS.TABLES.ADD ("Orders");

Datacolumn col = tbl.columns.add ("ORDERID", TypeOf (int));

Col.autoincrement = true;

Col.autoincrementSeed = -1;

Col.autoincrementstep = -1;

Col.readonly = true;

5, add an expression-based column

TBL.Columns.Add ("Itemtotal", TypeOf (Decimal), "Quantity * Unitprice");

6, modify DataTable content

1, add new DATAROW

DataRow Row = DS.Tables ["Customers"]. Newrow ();

ROW ["Customerid"] = "Alfki";

DS.Tables ["Customers"]. Rows.Add (Row);

Object [] avaleues = {"Alfki", "Alfreds", "Anders", "030-22222"}

Da.Tables ["Customers"]. loadingDataRow (Avalues, False);

2, modify the current line

The content of the modified line is forced to automatically modify the corresponding content in the database, and the modifications made to the row are considered to be the pending changes to the database using the SQLDataAdapter object.

DataRow RowCustom;

RowCustomer = DS.TABLES ["Custoemrs"]. Rows.Find ("Anton");

IF (RowCustomer == Null)

// No customer

Else

{

RowCustomer ["CompanyName"] = "newcompanyname";

RowCustomer ["ContactName"] = "NewContactName";

}

// Recommended this way

DataRow RowCustom;

RowCustomer = DS.TABLES ["Custoemrs"]. Rows.Find ("Anton");

IF (RowCustomer == Null)

// No customer

Else

{

Rowcustomer.BegineDit ();

RowCustomer ["CompanyName"] = "newcompanyname";

RowCustomer ["ContactName"] = "NewContactName";

Rowcustomer.endedit ();

}

// NULL means that the data is not modified

Obejct [] acustomer = {null, "newcompanyname", "newcontactname", null}

DataRow RowCustom;

RowCustomer = DS.TABLES ["Customers"]. Rows.Find ("Alfki"); RowCustomer.ItemArray = Acustom;

3, handle the null value of DATAROW

/ / View if it is empty

DataRow RowCustom;

RowCustomer = DS.TABLES ["Customers"]. Rows.Find ("Alfki");

IF (RowCustomer.Inull ("Phone"))

Console.writeline ("it's null");

Else

Console.writeline ("It's not null");

// Give null value

RowCustomer ["Phone"] = dbnull.value;

4, delete DATAROW

DataRow RowCustom;

RowCustomer = DS.TABLES ["Customers"]. Rows.Find ("Alfki");

Rowcustomer.delete ();

5, clear DATAROW

DataRow RowCustomer = DS.TABLES ["Customers"]. Rows.Find ("Alfki");

RowCustomer.itemarray = acuStomer;

Da.Tables ["Customers"]. Remove (RowCustomer);

or

DS.Tables ["Customers"]. Removeat (INTIndex);

6, use the DataRow.RowState property: unchanged, detached, added, modified, deleted

Private void demonstratROWState ()

{// Run a function to create a datatable with one column.dataable myTable = maketable (); DATAROW MyRow; // Create a new DataRow.myrow = myTable.NewRow (); // detached row.console.writeline ("New Row " myrow.rowstate; myTable.Rows.Add (MyRow); // new row.console.writeline (" addrow " myrow.rowstate; myTable.acceptchanges (); // unchanged row.console.writeline "Acceptchanges" myrow.rowstate); Myrow ["firstname"] = "scott"; // modified row.console.writeline ("Modified" myrow.rowstate; myrow.delete (); // deleted row.console .Writeline ("deleted" myrow.rowstate;

7 check the hang change in DATAROW

DataRow RowCustom;

RowCustomer = DS.TABLES ["Customers"]. Rows.Find ("Alfki");

Rowcustomer ["CompanyName"] = "newcompanyname"; string strnewcompanyname, StrolDcompanyName

Console.writeline (RowCustomer ["CompanyName", DataRowVersion.current]);

Console.writeline (RowCustomer ["CompanyName", DataRowVersion.original]);

Third, the attribute method introduction

1, DataSet

1, attribute

CaseSensitive: The string comparison is case sensitive to control the string comparison in DataTable.

DataSetName: The name of the current DataSet. If not specified, the attribute value is set to "NewDataSet". If you write the DataSet content to the XML file, DataSetName is the root node name of the XML file.

DesignMode: If you use the Dataset in the component, DesignMode returns true, otherwise it returns false.

HaserRORS: Indicates whether the DataRow object in the DataSet contains an error. If you submit a group of changes to the database and set the ContinueUpdateOrror property of the DataAPter object, you must check the DataSet's HaserRors property after submitting the changes to determine if there is an update failed.

Namespace and Prefix: Specify XML Namespaces and Prefix

Relations: Returns a DatarandCollection object.

TABLES: Check the existing DataTable object. Access DataTable via indexes has better performance.

2, method

AcceptChanges and RejectChanges: Accept or abandon all hang changes in the DataSet. When calling acceptchange, the ROWSTATE property value is the ROWState property of all rows of added or modified will be set to unchanged. Any DataRow object that is marked as deleted will be removed from the DataSet. When calling REJECTCHANGES, any DataRow object that is marked as added will be removed from the DataSet, and the other modified DATROW object will return the previous state.

CLEAR: Clear all DataRow objects in the DataSet. This method is fast than release a DataSet and then create a new DataSet of the same structure.

Clone and Copy: Use the Copy method to create a new DataSet with the original DataSet with the same structure and the same line. Use the Clone method to create a new DataSet with the same structure, but does not contain any rows.

GetChanges: Returns a new Dataset with the original DataSet object, and also contains all the rows that hang in the original DataSet.

Getxml and getXmlschema: Use the getXML method to get the contents of the DataSet with her architecture information into the XML format. If you only want to return to architecture information, you can use GetXmlschema.

Haschange: Indicates whether DataRow objects are included in the DataSet.

Merge: Load data from a set of DataRow objects in another DataSet, DataTable, or existing DataSet.

READXML and WRITEXML: Use the READXML method from files, TextReader, data streams, or XmlReader to load XML data into the DataSet.

Reset: Return DataSet as uninited state. If you want to abandon the existing DataSet and start processing new DataSet, use the reset method better than the new instance that creates a DataSet.

3, event

Mergefailed: Triggered when a DataSet's MERGE method is exception.

2, DataTable

1, attribute

2, method

3, event

ColumnChanged: Triggered after the content of the column is changed

COLUMNCHANGDING: Triggered before the content of the column is changed

Rowchanged, Rowchanging, Rowdeleted, Rowdeleting.

3, Datacolumn

1, attribute

4, Datarow

1, attribute

Haserror: Determine if the line contains an error.

Item: The content of the column is accessed by the number of columns, columns of columns or the name of the column or the Datacolumn object itself.

ItemArray: Gets or sets all columns in the row.

RowError: Returns a string containing row error messages.

RowState: Returns the value in the DataRowState enumeration to represent the current state of the line.

TABLE: Returns the DataTable in the DataRow object.

2, method

AcceptChanges and RejectChanges: Submit and give up hang changes.

Beginedit, Canceledit, Endedit

Clearerrors: Clear all the errors in DataRow.

DELETE: The delete method does not delete the DATAROW from the ROW collection of the DATAROW table. When calling the Detarow object's delete method, ADO.NET labeled the row as delete, then calls the Update method of the SQLDataAdapter object to delete its rows in the database.

If you want to completely delete DATAROW, you can call the Delete method, then call its AcceptECHANGES method, you can also use the Remove method of the DATAROWCOLLECTION object to complete the same task.

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

New Post(0)