Custom serialized dataset with structure.

Source: Internet
Author: User

(1). Description
Is there a problem in programming: Occasionally, for some reason, the database table needs to change the field name. For example, change: Name To: personname.
NextProgramAll involvedCodeOpen the file and find a file similar to DS. table [0]. row [N] ["name"] statement, modified to DS. table [0]. row [N] ["name"].
In fact, you don't need to do this. It's okay to add only one dataset with a structure. Modify only one attribute in the code file of this class. Besides this class code file
You do not need to modify any other application code.
(2). Code:

Using system;
Using system. Data;
Using system. runtime. serialization;

Namespace schemedataset
{
/// <Summary>
/// Custom serializable dataset containing customer information (Dataset)

/// </Summary>
[System. componentmodel. designercategory ("Code")]
[Serializableattribute]
Public class MERs: DataSet // inherits dataset, so that all methods in dataset can be called.
{
# Declaration of constants below Region

/// <Summary> constant defines the name of the customer table in the customer table. </Summary>
Public const string crmpscustomers_table = "customer ";

/// <Summary> This constant defines the column name of the customer number </Summary>
Public const string cusno_field = "cusno ";

/// <Summary> This constant defines the column name of the customer name </Summary>
Public const string customername_field = "customername ";

/// <Summary> This constant defines the column name of the customer address </Summary>
Public const string address_field = "Address ";

# Declaration of variables, constants, and objects below endregion

# The method declaration is as follows:

Public MERs ()
{
Builddatatables ();
}

/// <Summary>
/// Constructor supporting serialization

/// <Param name = "info"> Object serialization Information </param>
/// <Param name = "context"> context of the called method </param>
/// </Summary>
Private MERs (serializationinfo, streamingcontext context): Base (Info, context)
{}

/// <Summary>
/// Create a table with a structure: customer
/// </Summary>
Private void builddatatables ()
{
//
// Create a crmpscustomers data table
//

Datatable table = new datatable (crmpscustomers_table );
Datacolumncollection columns = table. columns;

// Define the 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 contact = new contact ();
This. Merge (contact); // merge
}

# Endregion: method declaration

}
}

OK.
(3). Use
1.
When such a class is defined, it should be used in the program as follows:
DS. Table [MERs. crmpscustomers_table]. Row [N] [customers. customername_field];
In this way, all the tasks are handed over to the code file of this class. When you modify a database, you do not need to modify statements similar to these statements. You only need to modify the values of the relevant properties of the structure class.
2.
Another scenario is to define a dataset with a structure. On some interfaces, the UI Layer interacts with the database in one way, that is, before data is added to the database.
If the architecture is not read, the dataset has no structure (no structure can store data), and this class takes effect.

Correct your criticism!

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.