Datatable adds columns, sets primary keys, adds rows, queries, updates datatable adds columns, sets primary keys, adds rows, queries, and updates

Source: Internet
Author: User
Datatable: add columns, Set primary keys, Add rows, query, and update

There are too many materials for database operations and you are familiar with them.
However, sometimes the data volume is small, but frequently updated variable operations usually use a custom structure, but the maintainability and flexibility of the custom structure is not as good as that of the temporary table, we can use datatable as a temporary memory table to flexibly add columns and rows as database operations to complete primary key settings, queries, updates, and other operations. It can also be saved as an XML file.

Suppose there is a global object in the current class scope.

Public   Static Datatable dtrefresh =   New Datatable ();

Add the following test to a test function:Code:

To facilitate the storage of XML files, you need to set the table name:

Dtrefresh. tablename =   " Refresh task " ;

Add a table structure for a temporary table, with column names:

Dtrefresh. Columns. Add ( " Quantity " , Typeof ( Int ));
Dtrefresh. Columns. Add ( " Frequency " , Typeof ( Int ));
Dtrefresh. Columns. Add ( " Auto-disable Delay " , Typeof ( Int ));
Dtrefresh. Columns. Add ( " URL " , Typeof ( String ));

Set the URL of a column as the primary key:

Datacolumn [] clos =   New Datacolumn [ 1 ];
Clos [ 0 ] = Dtrefresh. Columns [ " URL " ];
Dtrefresh. primarykey = C Los;

Add a test record for the table:
Note: When adding a row, you must declare the row and assign it to the object entity as needed. This aims to highlight that each time you add a new row, you only need to assign a dtrefresh. the object returned by newrow () does not need to be declared again. Because the class operation is to pass the reference, if a row object name is assigned to a table and the content of this object name is modified, the table content will be modified accordingly, therefore, you must reuse the row name every time you add rows, but avoid reusing objects.

Datarow Dr;
Dr = Dtrefresh. newrow ();
Dr [ " Quantity " ] =   100 ;
Dr [ " Frequency " ] =   100 ;
Dr [ " Auto-disable Delay " ] =   100 ;
Dr [ " URL " ] =   " Http://www.baidu.com/ " ;
Dtrefresh. Rows. Add (DR );

Run the query:

Datarow [] drarr = Dtrefresh. Select ( " Url = \ 'HTTP: // www.baidu.com /\' " );

Foreach(VAR itemInDrarr)
{
TXB. Text+ =Item ["URL"]. Tostring ()+Environment. newline;
}

Update records based on query conditions:

Datarow [] drarr = Dtrefresh. Select ( " Url = \ 'HTTP: // www.baidu.com /\' " );

Drarr [0] ["URL"]= "G.cn";

Foreach(VAR itemInDrarr)
{
TXB. Text+ =Item ["URL"]. Tostring ()+Environment. newline;
}

Write an XML file:

Dtrefresh. writexml ( @" D: \ 1.xml " );

There are too many materials for database operations and you are familiar with them.
However, sometimes the data volume is small, but frequently updated variable operations usually use a custom structure, but the maintainability and flexibility of the custom structure is not as good as that of the temporary table, we can use datatable as a temporary memory table to flexibly add columns and rows as database operations to complete primary key settings, queries, updates, and other operations. It can also be saved as an XML file.

Suppose there is a global object in the current class scope.

Public   Static Datatable dtrefresh =   New Datatable ();

Add the following test code to a test function:

To facilitate the storage of XML files, you need to set the table name:

Dtrefresh. tablename =   " Refresh task " ;

Add a table structure for a temporary table, with column names:

Dtrefresh. Columns. Add ( " Quantity " , Typeof ( Int ));
Dtrefresh. Columns. Add ( " Frequency " , Typeof ( Int ));
Dtrefresh. Columns. Add ( " Auto-disable Delay " , Typeof ( Int ));
Dtrefresh. Columns. Add ( " URL " , Typeof ( String ));

Set the URL of a column as the primary key:

Datacolumn [] clos =   New Datacolumn [ 1 ];
Clos [ 0 ] = Dtrefresh. Columns [ " URL " ];
Dtrefresh. primarykey = C Los;

Add a test record for the table:
Note: When adding a row, you must declare the row and assign it to the object entity as needed. This aims to highlight that each time you add a new row, you only need to assign a dtrefresh. the object returned by newrow () does not need to be declared again. Because the class operation is to pass the reference, if a row object name is assigned to a table and the content of this object name is modified, the table content will be modified accordingly, therefore, you must reuse the row name every time you add rows, but avoid reusing objects.

Datarow Dr;
Dr = Dtrefresh. newrow ();
Dr [ " Quantity " ] =   100 ;
Dr [ " Frequency " ] =   100 ;
Dr [ " Auto-disable Delay " ] =   100 ;
Dr [ " URL " ] =   " Http://www.baidu.com/ " ;
Dtrefresh. Rows. Add (DR );

Run the query:

Datarow [] drarr = Dtrefresh. Select ( " Url = \ 'HTTP: // www.baidu.com /\' " );

Foreach(VAR itemInDrarr)
{
TXB. Text+ =Item ["URL"]. Tostring ()+Environment. newline;
}

Update records based on query conditions:

Datarow [] drarr = Dtrefresh. Select ( " Url = \ 'HTTP: // www.baidu.com /\' " );

Drarr [0] ["URL"]= "G.cn";

Foreach(VAR itemInDrarr)
{
TXB. Text+ =Item ["URL"]. Tostring ()+Environment. newline;
}

Write an XML file:

Dtrefresh. writexml ( @" D: \ 1.xml " );

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.