Use annotation for typing

Source: Internet
Author: User

When the weak type variable is allowed for late binding access to the value,DatasetYou can also use a strong analogy to access data. With user-friendly names and strong type variables, you can accessDatasetTables and columns.

TypedDatasetYesfromDatasetDerived class. Similarly, it inheritsDatasetAll methods, events, and attributes. In additionDatasetProvides strongly typed methods, events, and attributes. This means that tables and columns can be accessed by name instead of using the set-based method. In additionCodeIn addition to readabilityDatasetYou can also allow the Visual Studio. NET code editor to automatically enter the rows you typed.

In additionDatasetIt also allows access to values of the correct type during compilation. By using a strongly typedDatasetWill capture Type Mismatch Errors During code compilation (rather than runtime.

Given an XML architecture that complies with the XML Schema Definition Language XSD standard, you can use the xsd.exe tool attached to the. NET Framework SDK to generate a strong data set.
The following code shows the syntax for generating dataset using this tool.
Xsd.exe/D/L: CS xsdschemafilename. XSD/N: xsdschema. namespace
In this syntax, the/d command instructs the tool to generate a dataset,/L: Tell the tool which language (such as C # Or Visual Basic. net) to use ). Optional/N: indicates that the tool also generates a namespace named xsdschema. namespace for dataset. The output of this command is xsdschemafilename. CS, which can be applied in ADO. net.Program . The generated code can be compiled into a library or module.
The following code uses the C # compiler (csc.exe) to compile the generated code into the library syntax.
Csc.exe/T: Library xsdschemafilename. CS/R: system. dll/R: system. Data. dll
/T: indicates that the tool is compiled into a database./R: Specifies the dependent database required for compilation. The output of this command is xsdschemafilename. dll, which can be passed to the compiler when the/R: command is used to compile the ADO. NET application.
The following code displays the syntax for accessing the namespace passed to xsd.exe In the ADO. NET application.
[C #]
Using xsdschema. namespace;
The following code uses a typed dataset named mermerdataset to load the list of customers in the northwind database. After data is loaded using the fill method, this example uses the typed mermersrow (datarow) object to cycle through each customer in the MERs table. It provides direct access to the customerid column, instead of accessing the column through datacolumncollection.
[C #]
Customerdataset custds = new customerdataset ();
Sqldataadapter custcmd = new sqldataadapter ("select * from MERs ",
"Data Source = localhost; Integrated Security = sspi; initial catalog = northwind ");

Custcmd. Fill (custds, "MERs ");

Foreach (customerdataset. customersrow custrow in custds. MERs mers)
Console. writeline (custrow. customerid );
The following is the XML architecture used for this example.
<? XML version = "1.0" encoding = "UTF-8"?>
<Xs: schema id = "customerdataset" xmlns = "" xmlns: xs = "http://www.w3.org/2001/XMLSchema" xmlns: msdata = "urn: Schemas-Microsoft-com: XML-msdata">
<Xs: element name = "customerdataset" msdata: isdataset = "true">
<Xs: complextype>
<Xs: Choice maxoccurs = "unbounded">
<Xs: element name = "customers">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "customerid" type = "XS: string" minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
</Xs: Choice>
</Xs: complextype>
</Xs: Element>
</Xs: schema>

Annotation allows you to modify the name of an element in a typed dataset without modifying the infrastructure. If you modify the name of an element in the infrastructure, the typed dataset will reference objects that do not exist in the data source, and the reference to objects that exist in the data source will be lost.
With Annotations, you can use more meaningful names to customize the names of objects in the typed dataset, making the code easier to read, and the typed dataset is easier to use for clients while keeping the infrastructure unchanged. For example, the following schema element of the MERs table in the northwind database generates the customersrow datarow object name and a datarowcollection named customers.
<Xs: element name = "customers">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "customerid" type = "XS: string" minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
The datarowcollection name customers is meaningful in the client code, but the datarow name customersrow will cause misunderstanding because it is a single object. In addition, normally, the object is referenced as a customer object instead of a row identifier. Solution: annotate the schema and identify the new name of the datarow and datarowcollection objects. The following is the annotated version of the previous architecture.
<Xs: element name = "customers" codegen: typedname = "customer" codegen: typedplural = "customers">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "customerid" type = "XS: string" minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
If the customer value is specified as typedname, The datarow object name customer is generated. If the value of MERs Mers is specified as typedplural, The datarowcollection name customers is retained.
The following table shows the available annotations.
Description
The name of the typedname object.
The name of the typedplural object set.
The name of the typedparent object when it is referenced in the parent relationship.
Typedchildren is used to return the name of the object method from the child relationship.
Nullvalue is a value if the base value is dbnull. For information about nullvalue annotation, see the following table. The default value is _ Throw.

The following table shows the values that can be specified for the nullvalue annotation.
Nullvalue description
Replace value to specify the value to be returned. The returned value must match the type of the element. For example, use nullvalue = "0" to return 0 for an empty integer field.
_ Throw raises an exception. This is the default value.
_ Null if the primitive type is encountered, null reference is returned or an exception is thrown.
_ Empty: Return string. empty for the string; otherwise, return the object created from the null constructor. If the primitive type is encountered, an exception is thrown.

The following table lists the default values and comments available for objects in a typed dataset.
Object/method/event default value Annotation
Datatabletablenamedatatabletypedplural
Datatable method newtablenamerow
Addtablenamerow
Deletetablenamerowtypedname
Datarowcollectiontablenametypedplural
Datarowtablenamerowtypedname
Datacolumndatatable. columnnamecolumn
Datarow. columnnametypedname
Propertypropertynametypedname
Child accessorgetchildtablenamerowstypedchildren
Parent accessortablenamerowtypedparent
Dataset event tablenamerowchangeevent
Tablenamerowchangeeventhandlertypedname

To use a typed dataset annotation, you must include the following xmlns references in the XML Schema Definition Language (XSD) architecture.
Xmlns: codegen = "urn: Schemas-Microsoft-com: XML-mspdrop"
The following is an example of the annotation architecture. It discloses the MERs table of the northwind database and contains the relationship with the orders table.
<? XML version = "1.0" encoding = "UTF-8"?>
<Xs: schema id = "customerdataset"
Xmlns: codegen = "urn: Schemas-Microsoft-com: XML-mspdrop"
Xmlns = ""
Xmlns: xs = "http://www.w3.org/2001/XMLSchema"
Xmlns: msdata = "urn: Schemas-Microsoft-com: XML-msdata">
<Xs: element name = "customerdataset" msdata: isdataset = "true">
<Xs: complextype>
<Xs: Choice maxoccurs = "unbounded">
<Xs: element name = "customers" codegen: typedname = "customer" codegen: typedplural = "customers">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "customerid" codegen: typedname = "customerid" type = "XS: string" minoccurs = "0"/>
<Xs: element name = "companyName" codegen: typedname = "companyName" type = "XS: string" minoccurs = "0"/>
<Xs: element name = "phone" codegen: typedname = "phone" codegen: nullvalue = "" type = "XS: string" minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
<Xs: element name = "orders" codegen: typedname = "order" codegen: typedplural = "orders">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "orderid" codegen: typedname = "orderid" type = "XS: int" minoccurs = "0"/>
<Xs: element name = "customerid" codegen: typedname = "customerid" codegen: nullvalue = "" type = "XS: string" minoccurs = "0"/>
<Xs: element name = "employeeid" codegen: typedname = "employeeid" codegen: nullvalue = "0" type = "XS: int" minoccurs = "0"/>
<Xs: element name = "orderdate" codegen: typedname = "orderdate" codegen: nullvalue = "1980-01-01t00: 00: 00" type = "XS: datetime "minoccurs =" 0 "/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
</Xs: Choice>
</Xs: complextype>
<Xs: Unique name = "constraint1">
<Xs: Selector XPath = ". // customers"/>
<Xs: Field XPath = "customerid"/>
</Xs: Unique>
<Xs: keyref name = "custorders" refer = "constraint1" codegen: typedparent = "customer" codegen: typedchildren = "getorders">
<Xs: Selector XPath = ". // orders"/>
<Xs: Field XPath = "customerid"/>
</Xs: keyref>
</Xs: Element>
</Xs: schema>
The following code uses a strongly-typed dataset created from the sample architecture. It uses a dataadapter to fill the MERs table and another dataadapter to fill the orders table. A strongly Typed Dataset defines datarelations.
[C #]
Sqlconnection nwindconn = new sqlconnection ("Data Source = localhost; Integrated Security = sspi; initial catalog = northwind ");
Sqldataadapter custda = new sqldataadapter ("select customerid, companyName, phone from customers", nwindconn );
Sqldataadapter orderda = new sqldataadapter ("select orderid, customerid, employeeid, orderdate from orders", nwindconn );

// Populate a strongly Typed Dataset.
Nwindconn. open ();
Customerdataset custds = new customerdataset ();
Custda. Fill (custds, "MERs ");
Orderda. Fill (custds, "orders ");
Nwindconn. Close ();

// Add a stronugly typed event.
Custds. Customers. customerchanged + = new
Customerdataset. customerchangeeventhandler (oncustomerchanged );

// Add a stronugly typed datarow.
Customerdataset. Customer newcust = custds. MERs. newcustomer ();
Newcust. customerid = "new01 ";
Newcust. companyName = "my new company ";
Custds. Customers. addcustomer (newcust );

// Navigate the Child relation.
Foreach (customerdataset. Customer customer in custds. MERs mers)
{
Console. writeline (customer. customerid );
Foreach (customerdataset. Order order in customer. getorders ())
Console. writeline ("\ t" + order. orderid );
}

Protected static void oncustomerchanged (Object sender, customerdataset. customerchangeevent E)
{

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.