快活林资源网 Design By www.csstdc.com

本文将介绍三种批量插入数据的方法。第一种方法是使用循环语句逐个将数据项插入到数据库中;第二种方法使用的是SqlBulkCopy,使您可以用其他源的数据有效批量加载 SQL Server 表;第三种使用的方法是sql server中的表值参数方法,表值参数是 SQL Server 2008 中的新参数类型。表值参数是使用用户定义的表类型来声明的。使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程或函数)发送多行数据。

      代码示例:

      此例子为控制台输出程序,有两个类,一个为BulkData类,主要实现了表值参数和sqlbulkcopy是如何插入数据的,一个类为Repository,一个app.config配置文件。所用数据库为sql server 2012。

       建库语句:

复制代码 代码如下:
打开
--Create DataBase
use master
go
if exists(select * from master.sys.sysdatabases where name=N'BulkDB')
drop database BulkDB
create database BulkDB;
go


--Create Table
use BulkDB
go

if exists(select * from sys.objects where object_id=OBJECT_ID(N'[dbo].[BulkTable]') and type in(N'U'))
drop table [dbo].BulkTable
Create table BulkTable(
Id int primary key,
UserName nvarchar(32),
Pwd varchar(16))
go


--Create Table Valued
use BulkDB
go

if exists
(
select * from sys.types st
join sys.schemas ss
on st.schema_id=ss.schema_id
where st.name=N'[BulkType]' and ss.name=N'dbo'
)
drop type [dbo].[BulkType]
go

create type [dbo].[BulkType] as table
  (
   Id int,
   UserName nvarchar(32),
   Pwd varchar(16)
   )
go

select * from dbo.BulkTable

BulkData.cs

复制代码 代码如下:
打开
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BulkData
{
    class BulkData
    {
        public static void TableValuedToDB(DataTable dt)
        {
            SqlConnection sqlConn = new SqlConnection(
              ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            const string TSqlStatement =
             "insert into BulkTable (Id,UserName,Pwd)" +
             " SELECT nc.Id, nc.UserName,nc.Pwd" +
             " FROM @NewBulkTestTvp AS nc";
            SqlCommand cmd = new SqlCommand(TSqlStatement, sqlConn);
            SqlParameter catParam = cmd.Parameters.AddWithValue("@NewBulkTestTvp", dt);

            catParam.SqlDbType = SqlDbType.Structured;

            catParam.TypeName = "dbo.BulkType";
            try
            {
                sqlConn.Open();
                if (dt != null && dt.Rows.Count != 0)
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sqlConn.Close();
            }
        }

        public static DataTable GetTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[]{new DataColumn("Id",typeof(int)),new DataColumn("UserName",typeof(string)),new DataColumn("Pwd",typeof(string))});

            return dt;
        }

        public static void BulkToDB(DataTable dt)
        {
            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn);
            bulkCopy.DestinationTableName = "BulkTable";
            bulkCopy.BatchSize = dt.Rows.Count;

            try
            {
                sqlConn.Open();
                if (dt != null && dt.Rows.Count != 0)
                    bulkCopy.WriteToServer(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sqlConn.Close();
                if (bulkCopy != null)
                    bulkCopy.Close();
            }
        }
    }
}

Repository.cs

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;

namespace BulkData
{
    public class Repository
    {
        public static void UseSqlBulkCopyClass()
        {
            Stopwatch sw = new Stopwatch();
            for (int outLayer = 0; outLayer < 10; outLayer++)
            {
                DataTable dt = BulkData.GetTable();
                for (int count = outLayer * 100000; count < (outLayer + 1) * 100000; count++)
                {
                    DataRow r = dt.NewRow();
                    r[0] = count;
                    r[1] = string.Format("User-{0}", count * outLayer);
                    r[2] = string.Format("Password-{0}", count * outLayer);
                    dt.Rows.Add(r);
                }
                sw.Start();
                BulkData.BulkToDB(dt);
                sw.Stop();
                Console.WriteLine(string.Format("{1} hundred thousand data elapsed Time is {0} Milliseconds", sw.ElapsedMilliseconds, outLayer + 1));
            }

            Console.ReadLine();
        }

        public static void UseTableValue()
        {
            Stopwatch sw = new Stopwatch();

            for (int outLayer = 0; outLayer < 10; outLayer++)
            {
                DataTable dt = BulkData.GetTable();

                for (int count = outLayer * 100000; count < (outLayer + 1) * 100000; count++)
                {
                    DataRow dataRow = dt.NewRow();
                    dataRow[0] = count;
                    dataRow[1] = string.Format("User-{0}", count * outLayer);
                    dataRow[2] = string.Format("Password-{0}", count * outLayer);
                    dt.Rows.Add(dataRow);
                }

                sw.Start();
                BulkData.TableValuedToDB(dt);
                sw.Stop();

                Console.WriteLine(string.Format("{1} hundred thousand data elapsed Time is {0} Milliseconds", sw.ElapsedMilliseconds, outLayer + 1));
            }

            Console.ReadLine();
        }

        public static void UserNormalInsert()
        {
            Stopwatch sw = new Stopwatch();

            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);

            SqlCommand sqlComm = new SqlCommand();
            sqlComm.CommandText = string.Format("insert into BulkTable(Id,UserName,Pwd)values(@p0,@p1,@p2)");
            sqlComm.Parameters.Add("@p0", SqlDbType.Int);
            sqlComm.Parameters.Add("@p1", SqlDbType.NVarChar);
            sqlComm.Parameters.Add("@p2", SqlDbType.VarChar);
            sqlComm.CommandType = CommandType.Text;
            sqlComm.Connection = sqlConn;
            sqlConn.Open();

            try
            {
                for (int outLayer = 0; outLayer < 10; outLayer++)
                {
                    for (int count = outLayer * 100000; count < (outLayer + 1) * 100000; count++)
                    {

                        sqlComm.Parameters["@p0"].Value = count;
                        sqlComm.Parameters["@p1"].Value = string.Format("User-{0}", count * outLayer);
                        sqlComm.Parameters["@p2"].Value = string.Format("Password-{0}", count * outLayer);
                        sw.Start();
                        sqlComm.ExecuteNonQuery();
                        sw.Stop();
                    }

                    Console.WriteLine(string.Format("{1} hundred thousand data elapsed Time is {0} Milliseconds", sw.ElapsedMilliseconds, outLayer + 1));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sqlConn.Close();
            }

            Console.ReadLine();
        }
    }
}

App.config

复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ConnStr"
      connectionString="data source=.;Integrated Security=SSPI;Initial Catalog=BulkDB"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

 Program.cs

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;

namespace BulkData
{
    class Program
    {           
        static void Main(string[] args)
        {
            //Repository.UseSqlBulkCopyClass();
            Repository.UseTableValue();
            //Repository.UserNormalInsert();
        }       
    }
}


三种方法分别插入100万条数据所用的时间为:

       循环语句所用时间:

       sql下三种批量插入数据的方法

        sqlbulkcopy方法所用时间为:

        sql下三种批量插入数据的方法

         表值参数所用时间为:

         sql下三种批量插入数据的方法

我不会告诉你有一种sql语法可以这么写:

复制代码 代码如下:
insert into SystemSet_tbl (ss_guid,ss_type,ss_comment) values ('00000000-0000-0000-0000-000000000007',1,''),('00000000-0000-0000-0000-000000000008',1,'')

快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。