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 = clos;
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 item in drArr)
{
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 item in drArr)
{
Txb. Text + = item ["Url"]. ToString () + Environment. NewLine;
}
Write an XML file:
DtRefresh. WriteXml (@ "d: \ 1.xml ");