Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand ADO.NET data sheet

Shulou Source: shulou.com Published: 2022-06-02 08:20:57 09月16日 Update

This article mainly explains "how to understand ADO. NET data table". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "how to understand ADO. NET data table" together!

ADO. NET contains a ColumnsCollection that represents the schema of the table. A data table also contains a RowsCollection that represents the data that the table holds. It remembers the initial state as well as the current state and keeps track of changes that have occurred. To use a data table, users must include System.Data.

Creating ADO. NET Data Tables

DataTable has two constructors: public DataTable() public DataTable(string tableName)

Add Columns to ADO. NET Data Tables

DataTable contains a collection of DataColumn objects. This collection of columns defines the structure of the table. To add a new column to the collection, use the Add method of the collection. In the following example, we add three columns to a data table using the Add method of the ColumnsCollection class; this method specifies the ColumnName and DataType attributes.

DataColumn dc = null; DataTable dt = new DataTable("test"); dc = dt.Columns.Add("CustID",System.Type.GetType("System.Int32") ); dc = dt.Columns.Add("CustomerNameLast", System.Type.GetType("System.String") ); dc = dt.Columns.Add("CustomerNameFirst", System.Type.GetType("System.String") ); dc = dt.Columns.Add("Purchases", System.Type.GetType("System.Double") );

The Add method of ColumnsCollection on DataTable has two overloaded functions:

Public DataColumn Add(String columnname, Type type) Public DataColumn Add(String columnname)

expression column

ADO. NET also allows users to create and define expression columns. Expressions in ADO+ are used to filter, calculate, and summarize column information. To create an expression column, set the DataType property to the type appropriate for the value returned by the expression; then set the Expression property to a valid expression:

DataColumn dc = New DataColumn; dc.DataType = System.Type.GetType("System.Currency"); dc.Expression = "total * .086";

You can also use the Add method to create an expression column. For example, the code below adds a column that calculates a discount based on 10% of the customer purchase amount. This expression multiplies the column named Purchases by 10%.

DataColumn dc = New DataColumn; dc= dt.Columns.Add("rectg", System.Type.GetType("System.Double"), "total * 0.1");

When the table is populated, the column values will be 10% of the total column values.

autoincrement column

Another feature of DataColumn is its ability to act as an autoincrement column. AutoIncrement Columns Automatically increments the values in a new column when you add it. To create an AutoIncrement column, you need to set the AutoIncrement property of the column to true. Once this property is set, the column begins with the value defined in the AutoIncrementSeed property of the column. When a column is added, the value of the AutoIncrement column is incremented by the value in the AutoIncrementStep property of that column.

dc = dt.Columns.Add("CustID",System.Type.GetType("System.Int32") ); dc.AutoIncrement = true; dc. AutoIncrementSeed = 1; dc.AutoIncrementStep = 1; Thank you for reading, the above is "how to understand ADO. NET data table" content, after the study of this article, I believe we have a deeper understanding of how to understand ADO. NET data table this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Data data tables expressions attributes values increments methods learning two content function state user face valid code example information customer object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Linux NVidia macOS