Controlling the Names Used in a Strongly Typed DataSet
Số trang: 5
Loại file: pdf
Dung lượng: 23.35 KB
Lượt xem: 9
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 2.18 Controlling the Names Used in a Strongly Typed DataSet Problem You want to assign your own names to the classes and properties for strongly typed DataSet classes. Solution Use annotations in the XML schema to control the names of classes and properties in strongly typed DataSet classes. The sample uses one XSD file: CategoriesDS_AnnotatedNa
Nội dung trích xuất từ tài liệu:
Controlling the Names Used in a Strongly Typed DataSet[ Team LiB ]Recipe 2.18 Controlling the Names Used in a Strongly Typed DataSetProblemYou want to assign your own names to the classes and properties for strongly typedDataSet classes.SolutionUse annotations in the XML schema to control the names of classes and properties instrongly typed DataSet classes.The sample uses one XSD file:CategoriesDS_AnnotatedName.xsd The schema used to generate the strongly typed DataSet. The schema is annotated so that you can access the collection of rows in the table by using the Categorys property of the DataSet rather than categories, each row by using the Category property of the row collection rather than CategoriesRow, and the CategoryName field by using the Name property of the row rather than CategoryName. The annotations are marked in bold in the Example 2-23.Example 2-23. File: TypedDataSetsCategoriesDS_AnnotatedName.xsd attributeFormDefault=qualified elementFormDefault=qualified> The sample code creates a strongly typed DataSet based on the Categories table inNorthwind. The user specifies whether the one based on the default or annotated schemafile is used. In either case, data is loaded into the DataSet and the collections of rows andcolumns in the DataSet are iterated over to display the data and to demonstrate the effectof the schema annotations.The C# code is shown in Example 2-24.Example 2-24. File: TypedDataSetNamesForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String CATEGORIES_TABLE = Categories;// . . .StringBuilder result = new StringBuilder( );// Create the DataAdapter.SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Categories, ConfigurationSettings.AppSettings[Sql_ConnectString]);if (annotatedRadioButton.Checked){ // Create the typed DataSet with name annotations. CategoriesDS_AnnotatedName ds = new CategoriesDS_AnnotatedName( ); // Fill the Categories table within DataSet. da.Fill(ds, CATEGORIES_TABLE); result.Append(Annotated Names + Environment.NewLine + Environment.NewLine); // Iterate over the rows collection and display columns. // Note that the row collection is Categorys // and that each row is Category. foreach(CategoriesDS_AnnotatedName.Category row in ds.Categorys) { // Note that the CategoryName field is referred to simply as Name. result.Append(row.CategoryID + + row.Name + + row.Description + Environment.NewLine); }}else{ // Create the typed DataSet without name annotations. CategoriesDS ds = new CategoriesDS( ); da.Fill(ds, CATEGORIES_TABLE); result.Append(Default + Environment.NewLine + Environment.NewLine); // Iterate over the rows collection and display columns. foreach(CategoriesDS.CategoriesRow row in ds.Categories) { result.Append(row.CategoryID + + row.CategoryName + + row.Description + Environment.NewLine); }}resultTextBox.Text = result.ToString( );DiscussionAnnotations are modifications to the XSD schema used to generate a strongly typedDataSet that allows the names of elements in the strongly typed DataSet to be customizedwithout changing the underlying schema. This allows more meaningful element names tobe used resulting in code that is easier to read, use, and maintain. Table 2-16 listsavailable annotations. Table 2-16. Available annotations Annotation DescriptiontypedChildren Name of the method that returns objects from a child data relation.typedName Name of the object.typedParent Name of the method that returns an object from a parent data relation.typedPlural Name of the collection of objects. Value or behavior if the underlying value is DBNull. Table 2-17 listsnullValue possible values for this annotation. The default value is _throw.Table 2-17 describes possible values for the nullValue annotation. Table 2-17. Values for nullValue annotation nullValue DescriptionReplacement A value having the same type as the element to be returnedValue Return String.Empty for a StringReturn an object created from an_empty empty constructor for other objectsThrow an exception for primitive types Return a null reference for objectsThrow an exception for primitive_null types_throw ...
Nội dung trích xuất từ tài liệu:
Controlling the Names Used in a Strongly Typed DataSet[ Team LiB ]Recipe 2.18 Controlling the Names Used in a Strongly Typed DataSetProblemYou want to assign your own names to the classes and properties for strongly typedDataSet classes.SolutionUse annotations in the XML schema to control the names of classes and properties instrongly typed DataSet classes.The sample uses one XSD file:CategoriesDS_AnnotatedName.xsd The schema used to generate the strongly typed DataSet. The schema is annotated so that you can access the collection of rows in the table by using the Categorys property of the DataSet rather than categories, each row by using the Category property of the row collection rather than CategoriesRow, and the CategoryName field by using the Name property of the row rather than CategoryName. The annotations are marked in bold in the Example 2-23.Example 2-23. File: TypedDataSetsCategoriesDS_AnnotatedName.xsd attributeFormDefault=qualified elementFormDefault=qualified> The sample code creates a strongly typed DataSet based on the Categories table inNorthwind. The user specifies whether the one based on the default or annotated schemafile is used. In either case, data is loaded into the DataSet and the collections of rows andcolumns in the DataSet are iterated over to display the data and to demonstrate the effectof the schema annotations.The C# code is shown in Example 2-24.Example 2-24. File: TypedDataSetNamesForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String CATEGORIES_TABLE = Categories;// . . .StringBuilder result = new StringBuilder( );// Create the DataAdapter.SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Categories, ConfigurationSettings.AppSettings[Sql_ConnectString]);if (annotatedRadioButton.Checked){ // Create the typed DataSet with name annotations. CategoriesDS_AnnotatedName ds = new CategoriesDS_AnnotatedName( ); // Fill the Categories table within DataSet. da.Fill(ds, CATEGORIES_TABLE); result.Append(Annotated Names + Environment.NewLine + Environment.NewLine); // Iterate over the rows collection and display columns. // Note that the row collection is Categorys // and that each row is Category. foreach(CategoriesDS_AnnotatedName.Category row in ds.Categorys) { // Note that the CategoryName field is referred to simply as Name. result.Append(row.CategoryID + + row.Name + + row.Description + Environment.NewLine); }}else{ // Create the typed DataSet without name annotations. CategoriesDS ds = new CategoriesDS( ); da.Fill(ds, CATEGORIES_TABLE); result.Append(Default + Environment.NewLine + Environment.NewLine); // Iterate over the rows collection and display columns. foreach(CategoriesDS.CategoriesRow row in ds.Categories) { result.Append(row.CategoryID + + row.CategoryName + + row.Description + Environment.NewLine); }}resultTextBox.Text = result.ToString( );DiscussionAnnotations are modifications to the XSD schema used to generate a strongly typedDataSet that allows the names of elements in the strongly typed DataSet to be customizedwithout changing the underlying schema. This allows more meaningful element names tobe used resulting in code that is easier to read, use, and maintain. Table 2-16 listsavailable annotations. Table 2-16. Available annotations Annotation DescriptiontypedChildren Name of the method that returns objects from a child data relation.typedName Name of the object.typedParent Name of the method that returns an object from a parent data relation.typedPlural Name of the collection of objects. Value or behavior if the underlying value is DBNull. Table 2-17 listsnullValue possible values for this annotation. The default value is _throw.Table 2-17 describes possible values for the nullValue annotation. Table 2-17. Values for nullValue annotation nullValue DescriptionReplacement A value having the same type as the element to be returnedValue Return String.Empty for a StringReturn an object created from an_empty empty constructor for other objectsThrow an exception for primitive types Return a null reference for objectsThrow an exception for primitive_null types_throw ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Controlling the Names Used in a Strongly Typed DataSetTài liệu liên quan:
-
52 trang 434 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 293 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0