Asp.Net使用Bulk批量插入数据_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Asp.Net使用Bulk批量插入数据

Asp.Net使用Bulk批量插入数据

 2014/9/2 21:33:48  Larry_blog  程序员俱乐部  我要评论(0)
  • 摘要:1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Web;5usingSystem.Diagnostics;6usingSystem.Data;7usingSystem.Data.SqlClient;8usingSystem.Configuration;9usingFx678Member.Framework.Exceptions;1011namespaceMeiYuanJinYe.Admin
  • 标签:.net ASP.NET 使用 net 数据
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Diagnostics;
 6 using System.Data;
 7 using System.Data.SqlClient;
 8 using System.Configuration;
 9 using Fx678Member.Framework.Exceptions;
10  
11 namespace MeiYuanJinYe.Admin.HttpHandler
12 {
13     /// <summary>
14     /// CreateAccount 的摘要说明
15     /// </summary>
16     public class CreateAccount : IHttpHandler
17     {
18  
19         public void ProcessRequest(HttpContext context)
20         {
21             context.Response.ContentType = "text/plain";
22             Guid classRoomId = Guid.Parse(context.Request["ClassRoomId"]);
23             int Count = int.Parse(context.Request["Count"]);
24             DataTable dt = GetTableSchema();
25             Random ran = new Random();
26             for (int i = 0; i < Count; i++)//循环往DataTable中赋值
27             {
28                 DataRow r = dt.NewRow();
29                 r[1] = ran.Next(10000000, 100000000);
30                 r[2] = ran.Next(10000000, 100000000);
31                 r[3] = classRoomId;
32                 r[4] = DateTime.Now;
33                 r[5] = 1;
34                 dt.Rows.Add(r);
35             }
36             BulkToDB(dt);
37             context.Response.Write(BulkToDB(dt) ? "ok" : "error");
38             context.Session["dataTable"] = dt;
39         }
40  
41  
42         public void BulkToDB(DataTable dt)
43         {
44             SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]);
45             SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn);
46             bulkCopy.DestinationTableName = "ClassRoomAccount";//数据库表
47             bulkCopy.BatchSize = dt.Rows.Count;
48             try
49             {
50                 sqlConn.Open();
51                 if (dt != null && dt.Rows.Count != 0)
52                     bulkCopy.WriteToServer(dt);
53             }
54             catch (Exception ex)
55             {
56                 new AppException("批量生成直播室账号异常", ex);
57             }
58             finally
59             {
60                 sqlConn.Close();
61                 if (bulkCopy != null)
62                     bulkCopy.Close();
63             }
64         }
65  
66         public DataTable GetTableSchema()
67         {
68             DataTable dt = new DataTable();
69             dt.Columns.AddRange(new DataColumn[]{  
70                 new DataColumn("AccountId",typeof(int)),  
71                 new DataColumn("AccountName",typeof(string)),  
72                 new DataColumn("Password",typeof(string)),
73                 new DataColumn("ClassRoomId",typeof(Guid)),
74                 new DataColumn("AddDate",typeof(DateTime)),
75                 new DataColumn("IsActive",typeof(int))
76             });//数据库表结构
77             return dt;
78         }
79         public bool IsReusable
80         {
81             get
82             {
83                 return false;
84             }
85         }
86     }
87 }

 

发表评论
用户名: 匿名