飞艳小屋

程序--人生--哲学___________________欢迎艳儿的加入

BlogJava 首页 新随笔 联系 聚合 管理
  52 Posts :: 175 Stories :: 107 Comments :: 0 Trackbacks

1.新的索引引擎更快的执行效率
   下面这段代码在2003中需要157秒,在2005中只要11秒就可以完成:

DataSet ds = new DataSet();

            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"));

            Cursor.Current 
= Cursors.WaitCursor;

            DateTime datBegin 
= DateTime.Now;

            Random rand 
= new Random();
            
int i, intValue;
            DataRow dr;

            
for (i = 1; i <= 500000; i++)
            
{
                
try
                
{
                    intValue 
= rand.Next();

                    dr 
= ds.Tables[0].NewRow();

                    dr[
"ID"= intValue;
                    dr[
"Value"= intValue;

                    ds.Tables[
0].Rows.Add(dr);
                }

                
catch { }
            }


            Cursor.Current 
= Cursors.Default;

            MessageBox.Show(
"Elapsed Time: " + (DateTime.Now - datBegin).Seconds.ToString());
            MessageBox.Show(
"count = " + ds.Tables[0].Rows.Count.ToString());
2.Dataset可以序列化为二进制文件
 string connstr = "server=(local);database=northwind;integrated security=true;async=true";

            DataSet ds 
= new DataSet();
            SqlDataAdapter dadpt 
= new SqlDataAdapter("select * from [order details]", connstr);
            dadpt.Fill(ds);

            BinaryFormatter bf 
= new BinaryFormatter();
            FileStream fs 
= new FileStream(@"c:\xml1.txt",FileMode.OpenOrCreate);

            ds.RemotingFormat = SerializationFormat.Binary;


            bf.Serialize(fs,ds);     
3.更独立的Datatable
  DataTable Write XML
 string connstr = "server=(local);database=northwind;integrated security=true;async=true";
            SqlDataAdapter dadpt 
= new SqlDataAdapter("select * from [order details]", connstr);
            DataTable dt 
= new DataTable("Customer");
            dadpt.Fill(dt);

            dt.WriteXml(
@"c:\DataTable.xml",true);
            dt.WriteXmlSchema(
@"c:\DataTableSchema.xml");
   DataTable Read XML
 StreamReader sr = new StreamReader(@"C:\DataTableSchema.xml");

            DataTable dt 
= new DataTable();
            dt.ReadXmlSchema(sr);

            dt.ReadXml(
new StreamReader(@"c:\dataTable.xml"));

            
this.dataGridView1.DataSource = dt;
    DataTable Merge
 string connstr = "server=(local);database=northwind;integrated security=true;async=true";
            SqlDataAdapter dadpt 
= new SqlDataAdapter("select * from customers", connstr);
            DataTable dt 
= new DataTable("Customer");
            dadpt.Fill(dt);

            SqlDataAdapter dadpt1 
= new SqlDataAdapter("select * from customers", connstr);
            DataTable dt1 
= new DataTable("Customer1");
            dadpt1.Fill(dt1);

            dt.Merge(dt1);

            
this.dataGridView1.DataSource = dt;
  DataTable Load DataReader
 string connstr = "server=(local);database=northwind;integrated security=true;async=true";
            SqlConnection conn 
= new SqlConnection(connstr);
            conn.Open();
            SqlCommand cmd 
= new SqlCommand("select * from [order details]", conn);
            SqlDataReader dr 
= cmd.ExecuteReader();

            DataTable dt 
= new DataTable("Customer");
            dt.Load(dr);

            
this.dataGridView1.DataSource = dt;
posted on 2005-12-08 14:05 天外飞仙 阅读(447) 评论(0)  编辑  收藏 所属分类: .net

只有注册用户登录后才能发表评论。


网站导航: