Names of columns in the custom database physical table

Source: Internet
Author: User

(1). prelude

1.
In actual software development, most of them are developed by team, which often occurs,
One programmer only designs a database and writes a program by another programmer.
Question. Design Database programmers design database Field naming conventions and write code
The naming conventions of a programmer's database are somewhat different. For example, database programmers learn about customer names.
Often named "customername", while programmers who write program code are used to naming "cusname ",
In this way, programmers who write program code may use the following code in a large number:
Dataset. Row [0] ["cusname"]. In this way, an error is reported during the update.
In this case, not only does it take time to modify bugs during development, but more importantly
This makes it difficult to unify the code pages of the entire project.
2. Client naming is not restricted by the database. For example
The two name fields are: customername and projectname; Client
On the relevant interface, when naming a field, you do not need to know the name of the field in which the table is written directly.
Name.
This article addresses the above two requirements.

(2) Solution

1.

In fact, sqldataadapter has already implemented this function. It can implement column ing, that is, database Columns

The columns customized by programmers correspond. Example:

There is a customer table. The following code maps the fields in the table:

Sqldataadapter myadapter = new dqldataadapter ();

Datatablemapping mymapping = new datatablemapping ();

Mymapping = myadapter. tablemappings. Add ("customer", "customer"); // notify the bridge adapter and add a ing object

Mymapping. columnmappings. Add ("customerid", "ID"); // The field name in the actual database is customerid.

// You can use the ID field name when operating on it. For example:

// Ds. Table ["customer"]. Row [0] ["ID"]; indicates

// Customer ID column

Mymapping. columnmappings. Add ("customername", "name ");

Mymapping. columnmappings. Add ("customeremail", "email ");

Myadapter. Fill (DS, "customer ");

You only need to use the ing operation for the above operations. The ing name can be customized. sqldataadapter will

They are regarded as the same column.

2. Solve the unified site problem:

A.

Assume that the code is used in ten pages:

DS. Table ["customer"]. Row [0] ["mermername"];

For some reason, to modify the database table fields, you must change the customername field in the physical database to the cusname field.

Based on the current situation, we need to go to the ten pages to find all:

DS. Table ["customer"]. Row [0] ["mermername"];

Change it to DS. Table ["customer"]. Row [0] ["cusname"];

Changing a field brings so much trouble. If you change multiple fields in multiple tables, how much time is consumed and maintenance is difficult?

Degree can be imagined. Here we will solve this problem.

B.

Define a class and inherit dataset. The Code is as follows:

Public Class Customer: Dataset

{

Public Enum Field

{

Id = 0; // these columns correspond to the actual field names after obtaining data from the dataset internal table.

Name = 1;

Email = 2;

Password = 3;

}

}

OK. The last step is as follows:

DS. Table ["customer"]. Row [0] [customer. Field. ID]; // obtain the value of a column.

Compared to DS. Table ["customer"]. Row [0] ["mermerid"];

Is it much more convenient?

In this way, if the physical database field is modified, the program code does not need to be modified at all (this class itself does not need to be modified ).

 

Thank you for reading!


 

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.