public ActionResult BatchCreateCoupons( BuildWithoutCouponsObject model)


        {


            var connect = ConfigurationManager .AppSettings["Connect" ].ToString(); //获取连接字符串


            SqlConnection sqlcon = new SqlConnection (connect);


            DateTime time = DateTime. Now;


            DataTable dt = new DataTable();


            dt .Columns. Add("Id", typeof (Guid));


            dt .Columns. Add("StoreGrouponId", typeof (Guid));


            dt .Columns. Add("BranchCode", typeof (string));


            dt .Columns. Add("StoreCode", typeof (string));


            dt .Columns. Add("CouponsStatus", typeof (CouponsStatus));


            dt .Columns. Add("CouponsCode", typeof (string));


            dt .Columns. Add("StoreId", typeof (Guid));


            dt .Columns. Add("TenantId", typeof (int));


            dt .Columns. Add("CreationTime", typeof (DateTime));


            dt .Columns. Add("CreatorUserId", typeof (long));


            dt .Columns. Add("LastModificationTime", typeof (DateTime));


            dt .Columns. Add("LastModifierUserId", typeof (long));


            for (int i = 1; i <= model.CreateCount; i ++)


            {


                string couponsCode = CommonUtil.GuidToStringID();


                DataRow r = dt.NewRow();


                r[ "Id" ] = Guid.NewGuid();


                r[ "StoreGrouponId" ] = model .Id;


                r[ "BranchCode" ] = model .TenancyName;


                r[ "StoreCode" ] = model .StoreCode;


                r[ "CouponsStatus" ] = CouponsStatus .Without;


                r[ "CouponsCode" ] = couponsCode;


                r[ "StoreId" ] = model .StoreId. Value;


                r[ "TenantId" ] = model .TenantId;


                r[ "CreationTime" ] = time;


                r[ "CreatorUserId" ] = model .CreatorUserId;


                r[ "LastModificationTime" ] = DBNull.Value;


                r[ "LastModifierUserId" ] = DBNull.Value;


                dt .Rows. Add(r);


            }


            sqlcon .Open();


            using (SqlBulkCopy bulk = new SqlBulkCopy (connect))


            {


                bulk .BatchSize = 1000;


                bulk .DestinationTableName = "Store_StoreGrouponCoupons" ;


                bulk .ColumnMappings.Add( "Id" , "Id");


                bulk .ColumnMappings.Add( "StoreGrouponId" , "StoreGrouponId" );


                bulk .ColumnMappings.Add( "BranchCode" , "BranchCode");


                bulk .ColumnMappings.Add( "StoreCode" , "StoreCode");


                bulk .ColumnMappings.Add( "CouponsStatus" , "CouponsStatus" );


                bulk .ColumnMappings.Add( "CouponsCode" , "CouponsCode" );


                bulk .ColumnMappings.Add( "StoreId" , "StoreId");


                bulk .ColumnMappings.Add( "TenantId" , "TenantId");


                bulk .ColumnMappings.Add( "CreationTime" , "CreationTime");


                bulk .ColumnMappings.Add( "CreatorUserId" , "CreatorUserId" );


                bulk .ColumnMappings.Add( "LastModificationTime" , "LastModificationTime" );


                bulk .ColumnMappings.Add( "LastModifierUserId" , "LastModifierUserId" );


                bulk .WriteToServer(dt);


               //ColumnMappings.Add("dt的列名","数据库表中对应的列名")


            }


            // DateTime endTime = DateTime.Now;


            // TimeSpan tt = time - endTime;


            //string tstr = tt.ToString();


            dt .Dispose();


            sqlcon .Close();


            sqlcon .Dispose();


            return null ;


        }  


 注:dt中的列表必须与目标表中的列名一一对应,SqlBulkCopy使用在包含事务的方法中的时候可能会发生冲突,会抱一个....不可用的错误,这个需要注意及解决.


可能是SqlBulkCopy会自动形成一个事务,事务中调事务可能就会报错,可能是工作单元的问题.BatchSize:为一次处理的行数,可以自定义


ConfigerManager.Setting("key"),取配置文件中的连接客串,如:< add key ="Connect" value="Data Source=120.26.107.74;database=FamiDB;pwd=Iamshbd5768;uid=Sa;" />