C#:30行数据插入到数据库中的效率测试-一行行执行、构造SQL一次执行、SqlBulkCopy
时间:2010-11-11 来源:Ferry
GridView中有30條記錄:
產品編號 產品名稱 產品價格
001 男士活力潔面乳 39
002 男士剃鬚刀 109
......
030 男士沐浴香波 120
有兩种方法寫入數據庫:
(1)
打開數據庫連接
逐條插入數據
關閉連接
(2)
拼湊出更新數據的SQL語句
打開數據庫連接
執行這條拼湊的SQL語句
關閉數據庫連接
請問哪一種效率更高?大概能高出多少?
另外,在ASP.NET 2.0中,有SqlBulkCopy類,它能完全取代普通的ADO.NET操作嗎?
于是我做了个简单测试,代码如下:
数据库表很简单:
CREATE TABLE [dbo].[TestTable]( [ID] [int] NULL, [CreateDateTime] [datetime] NULL, [TestMethod] [nvarchar](50) NULL ) ON [PRIMARY]
代码
using System;using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace InsertEfficiency
{
class Program
{
static void Main(string[] args)
{
//构造数据源
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID",typeof(int)));
dt.Columns.Add(new DataColumn("CreateDateTime",typeof(DateTime)));
dt.Columns.Add(new DataColumn("TestMethod", typeof(string)));
for (int i = 1; i <= 30; i++)
{
dt.Rows.Add(new object[] { i, DateTime.Now, "ExecuteSqlBulkCopy" });
}
Test t = new Test();
DateTime begin1 = DateTime.Now;
t.ExecuteRowByRow(dt);
DateTime end1 = DateTime.Now;
Console.WriteLine("ExecuteRowByRow:{0}ms", (end1 - begin1).Milliseconds);
DateTime begin2 = DateTime.Now;
t.ExecuteOnce(dt);
DateTime end2 = DateTime.Now;
Console.WriteLine("ExecuteOnce:{0}ms", (end2 - begin2).Milliseconds);
DateTime begin3 = DateTime.Now;
t.ExecuteSqlBulkCopy(dt);
DateTime end3 = DateTime.Now;
Console.WriteLine("ExecuteSqlBulkCopy:{0}ms", (end3 - begin3).Milliseconds);
Console.ReadLine();
}
}
class Test
{
public Test()
{
}
public void ExecuteRowByRow(DataTable dt)
{
using(SqlConnection conn = new SqlConnection(GetConnectionString))
{
conn.Open();
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow dr = dt.Rows[rowIndex];
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format("insert into TestTable values ({0},'{1}','{2}')",
dr[0].ToString(), dr[1].ToString(), "ExecuteRowByRow");
cmd.ExecuteNonQuery();
}
}
}
public void ExecuteOnce(DataTable dt)
{
StringBuilder strSql = new StringBuilder();
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow dr = dt.Rows[rowIndex];
string sql = string.Format("insert into TestTable values ({0},'{1}','{2}')",
dr[0].ToString(), dr[1].ToString(), "ExecuteOnce");
if (strSql.ToString().Length == 0)
{
strSql.Append(sql);
}
else
{
strSql.Append(";").Append(sql);
}
}
using (SqlConnection conn = new SqlConnection(GetConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strSql.ToString();
cmd.ExecuteNonQuery();
}
}
public void ExecuteSqlBulkCopy(DataTable dt)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString))
{
SqlBulkCopy bulk = new SqlBulkCopy(conn);
bulk.DestinationTableName = "TestTable";
bulk.BatchSize = dt.Rows.Count;
if (dt != null && dt.Rows.Count != 0)
{
conn.Open();
bulk.WriteToServer(dt);
}
bulk.Close();
}
}
string GetConnectionString
{
get
{
return @"server=.\mssqlserver2008;database=test;uid=sa;pwd=123456";
}
}
}
}
测试结果:
第一次执行ExecuteRowByRow:151msExecuteOnce:19msExecuteSqlBulkCopy:5ms
第二次执行ExecuteRowByRow:140msExecuteOnce:15msExecuteSqlBulkCopy:6ms
第三次执行ExecuteRowByRow:179msExecuteOnce:18msExecuteSqlBulkCopy:5ms
虽然测试方法比较简单,但基本能说明问题了。
相关阅读 更多 +