using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using TH.Com.General;
using TH.Common; public partial class EFUpdate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
var sp = new Stopwatch();
sp.Start(); var isSqlBulkCopy = true;
var list = new List<Article>();
using (var da = new THCommon())
{
Random rand = new Random(); for (var i = ; i < ; i++)
{
var art = new Article();
art.ID = Guid.NewGuid();
art.Area = Convert.ToDecimal(rand.Next() / 100.0);
art.CreateBy = "sdm";
art.CreateTime = DateTime.Now.AddMinutes(i);
art.Height = rand.Next();
art.Name = "Name" + i;
art.Note = "This is Note" + i;
art.Number = i;
art.Price = Convert.ToDecimal(rand.Next() / 100.0);
art.Weight = Convert.ToDecimal(rand.Next() / 100.0); list.Add(art); }
sp.Stop();
Debug.Print("Articles.Add = " + sp.ElapsedMilliseconds);
sp.Restart(); if (isSqlBulkCopy)
{
SaveList(da, list);
}
else
{
da.Articles.AddRange(list);
da.SaveChanges();
} sp.Stop();
Debug.Print("SaveChanges = " + sp.ElapsedMilliseconds);
sp.Restart();
}
} private void SaveList(DbContext db, List<Article> list)
{
//保存上传记录
var tableToInsert = Tools.ListToDataTable(list);
string cnstr = db.Database.Connection.ConnectionString;
using (var sqlBC = new SqlBulkCopy(cnstr))
{
sqlBC.DestinationTableName = "Art.Article";
sqlBC.BatchSize = ;
sqlBC.BulkCopyTimeout = ;
sqlBC.WriteToServer(tableToInsert);
}
} //更新测试
protected void Button2_Click(object sender, EventArgs e)
{
Debug.Print("测试数据更新方法速度比较");
Debug.Print("1.ef直接更新"); //EFUpdate1(10000);
//EFUpdate2(10000);
SqlDataAdapterUpdate();
} private void EFUpdate1(int count)
{
var sp = new Stopwatch();
sp.Start();
using (var da = new THCommon())
{
var list = da.Articles.Take().ToList(); sp.Stop();
Debug.Print("ToList = " + sp.ElapsedMilliseconds);
sp.Restart(); list.ForEach(t => t.Area = t.Height * t.Price);
sp.Stop();
Debug.Print("ForEach = " + sp.ElapsedMilliseconds);
sp.Restart(); da.SaveChanges(); sp.Stop();
Debug.Print("SaveChanges = " + sp.ElapsedMilliseconds);
sp.Restart();
}
} private void EFUpdate2(int count)
{
var sp = new Stopwatch();
sp.Start();
using (var da = new THCommon())
{
var list = da.Articles.Take().ToList(); var sql = "update art.article set Area = Height * Price where createtime<'2017-01-07 10:13:54.870' "; da.Database.ExecuteSqlCommand(sql); sp.Stop();
Debug.Print("ExecuteSqlCommand = " + sp.ElapsedMilliseconds);
sp.Restart();
}
} private void SqlDataAdapterUpdate()
{
var sp = new Stopwatch();
sp.Start();
using (var da = new THCommon())
{
var conn = (SqlConnection)da.Database.Connection; //设置select查询命令,SqlCommandBuilder要求至少有select命令
var selectCMD = new SqlCommand("select * from art.article where createtime<'2017-01-07 10:13:54.870'", conn);
var dt = new DataTable();
var sda = new SqlDataAdapter(selectCMD); //上面的语句中使用select 0,不是为了查询出数据,而是要查询出表结构以向DataTable中填充表结构
sda.Fill(dt); foreach (DataRow row in dt.Rows)
{
row["Area"] = Convert.ToDecimal(row["Height"]) * Convert.ToDecimal(row["Price"]);
}
//SqlCommandBuilder scb = new SqlCommandBuilder(sda); var dtc = dt.GetChanges();
SqlCommandBuilder scb = new SqlCommandBuilder(sda); sp.Stop();
Debug.Print("SqlCommandBuilder = " + sp.ElapsedMilliseconds);
sp.Restart(); if (dtc != null)
{
//执行更新
sda.Update(dtc);
} ////使DataTable保存更新
//dt.AcceptChanges(); sp.Stop();
Debug.Print("sda.Update = " + sp.ElapsedMilliseconds);
sp.Restart();
} } protected void Button3_Click(object sender, EventArgs e)
{
var sp = new Stopwatch();
sp.Start(); var list = new List<Article>(); var isDeleteSql = true; using (var da = new THCommon())
{
if (isDeleteSql)
{
var start = Convert.ToDateTime("2017-01-16");
var end = Convert.ToDateTime("2017-01-18"); var toDeleteListIDS =
da.Articles.Where(
t =>
t.CreateTime > start &&
t.CreateTime < end).Select(t => t.ID).ToList();
var ids = string.Join("','", toDeleteListIDS.ToArray());
var sql = string.Format("delete art.article where id in ('{0}') ", ids); da.Database.ExecuteSqlCommand(sql); sp.Stop();
Debug.Print("ExecuteSqlCommand delete = " + sp.ElapsedMilliseconds);
sp.Restart();
}
else
{
var dels = da.Articles.Take(); da.Articles.RemoveRange(dels);
da.SaveChanges();
sp.Stop();
Debug.Print("RemoveRange = " + sp.ElapsedMilliseconds);
sp.Restart();
} }
} /// <summary>
/// 先删除,再插入,在事务中执行
/// </summary>
private void DeleteAndInsert()
{
var rand = new Random();
using (var da = new THCommon())
{
using (System.Data.Entity.DbContextTransaction dbTran = da.Database.BeginTransaction())
{
try
{
var start = Convert.ToDateTime("2017-01-20");
var end = Convert.ToDateTime("2017-01-21"); var list = da.Articles.Where(t => t.CreateTime > start && t.CreateTime < end).ToList();
var ids = string.Join("','", list.Select(t => t.ID).ToArray()); //删除
var sql = string.Format("delete art.article where id in ('{0}') ", ids);
da.Database.ExecuteSqlCommand(sql); list.ForEach(delegate(Article art)
{
art.Height = ;
art.Number = ;
art.Weight = ;
art.Area = rand.Next();
}); SaveList(da, list); //commit transaction
dbTran.Commit();
}
catch (Exception ex)
{
//Rollback transaction if exception occurs
dbTran.Rollback();
} } }
}
protected void Button4_Click(object sender, EventArgs e)
{
DeleteAndInsert();
}
   protected void Button5_Click(object sender, EventArgs e)
{
//1.删除临时表
//2.选择记录,并修改,并批插入表临时表
//3.update from var sp = new Stopwatch();
sp.Start(); using (var da = new THCommon())
{
var list = da.Articles.Take(100000).ToList(); sp.Stop();
Debug.Print("ToList = " + sp.ElapsedMilliseconds);
sp.Restart(); list.ForEach(delegate(Article art)
{
art.Area = 1;
art.Height = 2;
art.Number = 3;
art.Price = 4;
art.Weight = 5;
}); sp.Stop();
Debug.Print("ForEach = " + sp.ElapsedMilliseconds);
sp.Restart(); var sql = "truncate table art.articletmp";
da.Database.ExecuteSqlCommand(sql); sp.Stop();
Debug.Print("ExecuteSqlCommand truncate = " + sp.ElapsedMilliseconds);
sp.Restart(); //保存上传记录
var tableToInsert = Tools.ListToDataTable(list);
string cnstr = da.Database.Connection.ConnectionString;
using (var sqlBC = new SqlBulkCopy(cnstr))
{
sqlBC.DestinationTableName = "Art.ArticleTmp";
sqlBC.BatchSize = 10000;
sqlBC.BulkCopyTimeout = 300;
sqlBC.WriteToServer(tableToInsert);
} sp.Stop();
Debug.Print(" SqlBulkCopy = " + sp.ElapsedMilliseconds);
sp.Restart(); sql = "update art.article set area = t0.area,height=t0.height,number = t0.number,price = t0.price,weight = t0.weight from art.articletmp t0 where art.article.id = t0.id";
da.Database.ExecuteSqlCommand(sql);
sp.Stop();
Debug.Print("ExecuteSqlCommand update = " + sp.ElapsedMilliseconds);
sp.Restart(); }
}

  

}

EFUpdate的更多相关文章

  1. 测试EntityFramework,Z.EntityFramework.Extensions,原生语句在不同的查询中的表现。原来池化与非池化设定是有巨大的影响的。

    Insert测试,只测试1000条的情况,多了在实际的项目中应该就要另行处理了. using System; using System.Collections.Generic; using Syste ...

  2. Unity框架入门

    介绍Unity框架之前,先要说几个概念DIP依赖倒置原则.IOC控制反转.DI依赖注入 DIP是设计原则之一,定义:上层不应该依赖于底层,两者都依赖于抽象: 抽象不依赖于细节,细节应该依赖于抽象. 像 ...

  3. ADO,net 实体数据模型增、删、改,浅谈

    第一步:建立ADO.net数据模型,一步步操作就行 第二步:画个简单的测试界面 第三步铺代码 using DevComponents.DotNetBar.SuperGrid; using DevCom ...

随机推荐

  1. Mysql查询——深入学习

    1.开篇 之前上一篇的随笔基本上是单表的查询,也是mysql查询的一个基本.接下来我们要看看两个表以上的查询如何得到我们想要的结果. 在学习的过程中我们一起进步,成长.有什么写的不对的还望可以指出. ...

  2. "Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated )

    "Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated ...

  3. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  4. 开源遥感平台opticks插件开发指南

    Opticks是一款开源的遥感数据处理平台,与其同类开源软件OSSIM一样,支持种类丰富的数据文件格式,但其最大特点为设计精巧的插件开发模式,在设计技巧上,系统提供了良好的封装特性,即使插件开发者对框 ...

  5. IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换

    /* * 用于实现 IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换的帮助类 */ using System;using System.IO;us ...

  6. Shiro-HelloWord

    HelloWorld Shiro的HelloWorld不是我们写的,而是看Shiro给我们提供的一段代码.通过这段代码可以看到Shiro大致的使用方式. 1.找到Shiro的jar包 目前的最新稳定版 ...

  7. js修改伪类的值

    css文件 p.change:after { content: attr(data-content); } js文件 $(this).addClass('change').attr('data-con ...

  8. git: 修改commiter 信息

    Committer: root root@localhost.localdomain 您的姓名和邮件地址基于登录名和主机名进行了自动设置.请检查它们正确 与否.您可以通过下面的命令对其进行明确地设置以 ...

  9. php 远程本地化无后缀图片

    $body = '<p style="margin: 0px 0px 1.1em; padding: 0px; color: rgb(51, 51, 51); font-family: ...

  10. View绘制机制

    View 绘制机制 1. View 树的绘图流程 当 Activity 接收到焦点的时候,它会被请求绘制布局,该请求由 Android framework 处理.绘制是从根节点开始,对布局树进行 me ...