C# DataSet performance best practices

黄舟
Release: 2017-02-13 11:56:28
Original
2163 people have browsed it

C# Performance optimization details

##1. Use ItemArray to implement batch assignment to DataRow


  • When assigning values ​​to all fields of DataRow, using field names for column-by-column assignment is inefficient. At this time, batch field assignment should be used whenever possible. You can use the ItemArray or rows.Add method:

    / ds是数据集(DataSet)对象
    DataTable dt = ds.Tables[0];
    DataRow row = dt.NewRow();
    row.ItemArray = new object[] { value1, value2, …, valuen };
    // ds是数据集(DataSet)对象
    DataTable dt = ds.Tables[0];
    dt.Rows.Add(value1, value2, …, valuen);
    //应避免做大量连续的单列赋值,如下:
    DataTable dt = ds.Tables[0];
    DataRow row = dt.NewRow();
    row["col1"] = value1;
    row["col2"] = value2;
    …
    row["coln"] = valuen;
    Copy after login

2. Reasonable use of parallel computing of DataTable


  • DataTable's built-in parallel computing can make full use of each CPU of the computer to optimize efficiency.

    IEnumerable<DataRow> FindRows() //查找所有数量小于0的分录
    {
        DataTable dt = ItemDataTable;
        ……
        return dt.Select(“Quantity<0”); //未使用并行计算
    }
    IEnumerable<DataRow> FindRows() //查找所有数量小于0的分录
    {
        DataTable dt = ItemDataTable;
        ……
        int index = dt.Columns.IndexOf("Quantity");
        return dt.AsEnumerable().AsParallel().Where(dr => (decimal)dr[index] < 0); //使用并行计算:
    }
    Copy after login



  • ##According to experiments, when the DataTable Parallel computing is better than Select and loop filtering when row selection; the performance is similar when row traversal is performed.



##3. Use ImportRow to merge into DataTable with the same structure


    Using the Merge method can easily achieve the merging of DataTables, but the efficiency of Merge is very poor; the example is as follows:
  • DataTable[] srcTables = ... ;
    foreach(DataTable src in srcTables )
    {	
    	dest.Merge( src ) ;
    }
    Copy after login

  • ImportRow can also implement the merge operation of DataTable, and its performance is much higher than that of Merge. The code example is as follows:
  • DataTable[] srcTables = ... ;
    foreach(DataTable src in srcTables )
    {
      foreach(DataRow row in src.Rows)
      {
         dest.ImportRow( row ) ;      
      }
    }
    Copy after login


  • 4. To be continued


The above is the content of C# DataSet performance best practices. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!