Customizing a serializable dataset with structured datasets

Source: Internet
Author: User
Tags define constant modify object serialization serialization
Data

 (i). Description

There is a problem with programming: Occasionally, for some reason, a database table needs to be changed to a field name. For example: change: Name to: PersonName.

The programmer then turns on all the code involved and finds something similar: DS. Table[0]. row[n][the statement "Name", modified to: DS. Table[0]. row[n]["Name"].

In fact, there is no need to do so, just add a structure with a dataset on the OK. Modifying only one attribute in this class of code file is OK, and the application code other than the code file in this class is not modified at all.

 (ii). Code:

Using System;
Using System.Data;
Using System.Runtime.Serialization;

Namespace Schemedataset
{
<summary>
A custom serializable dataset (dataset) that contains customer information

</summary>
[System.ComponentModel.DesignerCategory ("Code")]
[SerializableAttribute]
public class Customers:dataset//inheritance dataset, so that all methods in the dataset can be called
{
#region the following declarations for constants

<summary> constants define table names for customer data table customers </summary>
Public Const string crmpscustomers_table = "Customer";

<summary> This constant defines the field name of the customer number </summary>
Public Const string Cusno_field = "Cusno";

<summary> The constant defines the field name of the customer name </summary>
Public Const string Customername_field = "CUSTOMERNAME";

<summary> This constant defines the field name of the customer address </summary>
Public Const String Address_field = ' address ';

#endregion the following variables, constants, and declarations of objects

#region the following method declarations

Public Customers ()
{
Builddatatables ();
}

<summary>
Constructors that support serialization

<param name= "Info" > object serialization Information </param>
<param name= "Context" > contextual </param> about the method of being called
</summary>
Private Customers (SerializationInfo info, StreamingContext context): Base (info, context)
{}

<summary>
To create a table with a structure: Customer
</summary>
private void Builddatatables ()
{
//
Create a crmpscustomers datasheet
//

DataTable table = new DataTable (crmpscustomers_table);
datacolumncollection columns = table. Columns;

Define Structure
Columns. ADD (cusno_field,typeof (System.String));
Columns. ADD (customername_field,typeof (System.String));
Columns. ADD (address_field,typeof (System.String));

This. Tables.add (table);
Contact contacts = new Contact ();
This.   Merge (contact); Merge
}

#endregion the following method declarations

}
}

 (iii). Use

1. When such a class is defined, it should be used in the program:

Ds. Table[customers.crmpscustomers_table]. Row[n][customers.customername_field];

This will give all the tasks to this class code file. When you modify a database, you do not have to modify the same statements. Just modify the value of the structure class-related property.

2. Defines the data set for this band structure, there is also a situation where the UI layer interacts with the database in a one-way way, that is, without reading the schema before adding data to the database, then the dataset has no structure (no structure cannot store data), and the class works. Thank you!



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.