.NET common operation in the DataTable

First, the purpose

  In various .NET development, DataTable is a very common and important type, in the process of dealing with the data objects can be said to be essential.

  It is powerful, attributes and functions is quite rich, with good, so we deal with data, reduce a lot of workload and improve work efficiency. It is a feature-rich help us solve a lot of problems, but also increases the difficulty of memory, learning and remembering previous method, useless for some time to forget, such as when then need it, there is need to Baidu or Google , more waste of time. Therefore, there will be a variety of operating under common scenarios DataTable record, one is easily review learning, while the second is access to facilitate the work.

  But, because of lack of experience, and less familiar usage scenarios, and therefore in each encounter a new usage scenarios and feel very typical, in the future may be used, and then increase. If you see other expert methods more sophisticated, the existing operation will be updated. So this blog post is a long-term task, requiring time and effort to improve slowly.

Two, DataTable class common operations

  1, create the table structure and inserting data

    Create a table in this way is rarely used in actual development work, but also need to be familiar, does not mean that it will not be used rarely used, such as yourself to do some testing time. This is too basic, if not a little embarrassed to hand write it.

            // Create an empty table 
            the DataTable dt = new new the DataTable (); 
            the DataColumn column; 
            the DataRow Row; 

            // Create a first row 
            column = new new the DataColumn (); 
            column.DataType = System.Type.GetType ( " System.Int32 The " ); 
            column.ColumnName = " ID " ; 
            column.ReadOnly = to true ; 
            column.Unique = to true ; 
            dt.Columns.Add (column); 
            // Create a second column
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "NAME";
            dt.Columns.Add(column);
            //创建第三列
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "AGE";
            dt.Columns.Add(column);

            //创建行
            row = dt.NewRow();
            row["ID"] = 1;
            row["NAME"] = "小明";
            row["AGE"] = "18";
            dt.Rows.Add(row);

  2, copy the table structure

      This operation copies only the table structure, do not copy the data to obtain an empty table already contains a column, so that the operation of the above minus the cumbersome create columns.

            DT2 = the DataTable dt.Clone (); // dt is here has been built above 
            the DataRow Row = dt2.NewRow (); 
            Row [ " ID " ] = 2 ; 
            Row [ " NAME " ] = " Hill " ; 
            Row [ " of AGE " ] = " 15 " ; 
            dt2.Rows.Add (Row);         

 (To be continued)

Guess you like

Origin www.cnblogs.com/wangyihome/p/11260923.html