ADO.NET 2.0 中的新增 DataSet 功能性能提高44倍?(GridView VS Datagrid)

ADO.NET 2.0 中的新增 DataSet 功能--MSDN(downmoon翻译成Csharp)
  原文地址:http://www.microsoft.com/china/MSDN/library/data/dataAccess/adonetdatasetenhance.mspx?mfr=true

  本文主要帮助读者了解有关 .NET Framework DataSet 类以及与它密切相关的类中的新增 ADO.NET 2.0 功能的知识。这些更改包括对 DataSet、DataTable 和 DataView 类的功能和性能增强。


  因为原文代码用的是vb.net,阅读起来不便,邀月翻译成Csharp2005,方便大家阅读。呵呵!


 private void button1_Click(object sender, EventArgs e)
        
{
            
//下列代码在vs2005下执行40秒!
            //在vs2003下执行30分钟!!增加44倍!!

            DataSet ds = new DataSet();
            System.DateTime time1 
= new System.DateTime();
            System.TimeSpan ts 
= new TimeSpan();
            System.Timers.Timer tm 
= new System.Timers.Timer();
            tm.Enabled 
= true;
            tm.Interval 
= 1000;
            
int i;
            
int SPsecond = 0;
            DataRow dr;
            ds.Tables.Add(
"BigTable");
            ds.Tables[
0].Columns.Add("ID", Type.GetType("System.Int32"));
            ds.Tables[
0].Columns["ID"].Unique = true;
            ds.Tables[
0].Columns.Add("Value", Type.GetType("System.Int32"));
            WaitLabel.Visible 
= true;
            
this.Cursor = Cursors.WaitCursor;
            
this.Refresh();
            time1 
= DateTime.Now;
            Random rand 
= new Random();
            
//Random ri = new Random(unchecked((int)DateTime.Now.Ticks));
            int value;
            
for (int k = 1; k <= 1000000; k++)
            
{
                
try
                
{
                    value 
= rand.Next();
                    dr 
= ds.Tables[0].NewRow();
                    dr[
"ID"= value;
                    dr[
"Value"= value;
                    ds.Tables[
0].Rows.Add(dr);
                }

                
catch (Exception ex)
                
{
                }

            }

            WaitLabel.Visible 
= false;
            
this.Cursor = this.DefaultCursor;
            ts 
= DateTime.Now - time1;
            SPsecond 
= ts.Seconds;
            MessageBox.Show(
"Elapsed Time: " + SPsecond.ToString() + "  Seconds");
            MessageBox.Show(
"count = " + ds.Tables[0].Rows.Count);
        }

        
private void button2_Click(object sender, EventArgs e)
        
{
            XMLFormat();
        }

        
private void XMLFormat()
        
{
            DataSet ds 
= new DataSet();
            System.Data.SqlClient.SqlDataAdapter da 
= new System.Data.SqlClient.SqlDataAdapter("select * from [order details]", GetConnectionString());
            da.Fill(ds);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf 
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            
string FileName = "c:\" + DateTime.Now.ToString("yyyyMMddhhmmsslll"+ "xml20.txt";
            System.IO.FileStream fs 
= new System.IO.FileStream(FileName, System.IO.FileMode.CreateNew);
            bf.Serialize(fs, ds);
            MessageBox.Show(
"生成成功" + FileName + " " + "文件大小(b):" + fs.Length.ToString());
        }

        
private void BinaryFormat()
        
{
            DataSet ds 
= new DataSet();
            System.Data.SqlClient.SqlDataAdapter da 
= new System.Data.SqlClient.SqlDataAdapter("select * from [order details]", GetConnectionString());
            da.Fill(ds);
            
            
//下句使生成数据减少80%左右
            ds.RemotingFormat = SerializationFormat.Binary;

            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf 
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            
string FileName = "c:\" + DateTime.Now.ToString("yyyyMMddhhmmsslll"+ "Binary20.txt";
            System.IO.FileStream fs 
= new System.IO.FileStream(FileName, System.IO.FileMode.CreateNew);

            bf.Serialize(fs, ds);
            MessageBox.Show(
"生成成功" + FileName + " " + "文件大小(b):" + fs.Length.ToString());

        }

        
private string GetConnectionString()
        
{
            
//return MySettings.Value.NorthwindConnection;
            return "server=10.0.0.121;database=northwind;uid=sa;pwd=sa";
        }

        
private void button3_Click(object sender, EventArgs e)
        
{
            BinaryFormat();
        }

        
private void button4_Click(object sender, EventArgs e)
        
{
            getGridViewData();
        }

        
private void getGridViewData()
        
{
            
try
            
{
                SqlConnection connection 
= new SqlConnection(GetConnectionString());
                SqlCommand command 
= new SqlCommand("SELECT * from customers", connection);
                connection.Open();
                System.Data.SqlClient.SqlDataReader dr 
= command.ExecuteReader();
                
//Fill table with data from DataReader
                System.Data.DataTable dt = new DataTable();
                dt.Load(dr, LoadOption.OverwriteChanges);

                
// Display the data
                dataGridView1.DataSource = dt;

            }


            
catch (SqlException ex)
            
{
                MessageBox.Show(ex.Message);

            }

        }

  邀月总结:2.0对1.1的改进,不仅在DataSet,同样在GridView对于DataGrid性能的改进。应尽量摒弃Datagrid!这也是微软在2.0下默认不加载DataGrid的原因。

邀月注:本文版权由邀月和博客园共同所有,转载请注明出处。
助人等于自助!  3w@live.cn
原文地址:https://www.cnblogs.com/downmoon/p/1019829.html