欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)

 更新時(shí)間:2014年09月03日 08:59:54   投稿:shichen2014  
這篇文章主要介紹了Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)的方法,對(duì)于進(jìn)行asp.net數(shù)據(jù)庫(kù)程序設(shè)計(jì)非常有借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)的方法,分享給大家供大家參考之用。具體方法如下:

主要功能代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Fx678Member.Framework.Exceptions;
 
namespace MeiYuanJinYe.Admin.HttpHandler
{
  /// <summary>
  /// CreateAccount 的摘要說(shuō)明
  /// </summary>
  public class CreateAccount : IHttpHandler
  {
 
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      Guid classRoomId = Guid.Parse(context.Request["ClassRoomId"]);
      int Count = int.Parse(context.Request["Count"]);
      DataTable dt = GetTableSchema();
      Random ran = new Random();
      for (int i = 0; i < Count; i++)//循環(huán)往DataTable中賦值
      {
        DataRow r = dt.NewRow();
        r[1] = ran.Next(10000000, 100000000);
        r[2] = ran.Next(10000000, 100000000);
        r[3] = classRoomId;
        r[4] = DateTime.Now;
        r[5] = 1;
        dt.Rows.Add(r);
      }
      BulkToDB(dt);
      context.Response.Write(BulkToDB(dt) ? "ok" : "error");
      context.Session["dataTable"] = dt;
    }
 
    public void BulkToDB(DataTable dt)
    {
      SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]);
      SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn);
      bulkCopy.DestinationTableName = "ClassRoomAccount";//數(shù)據(jù)庫(kù)表名
      bulkCopy.BatchSize = dt.Rows.Count;
      try
      {
        sqlConn.Open();
        if (dt != null && dt.Rows.Count != 0)
          bulkCopy.WriteToServer(dt);
      }
      catch (Exception ex)
      {
        new AppException("批量生成直播室賬號(hào)異常", ex);
      }
      finally
      {
        sqlConn.Close();
        if (bulkCopy != null)
          bulkCopy.Close();
      }
    }
 
    public DataTable GetTableSchema()
    {
      DataTable dt = new DataTable();
      dt.Columns.AddRange(new DataColumn[]{ 
        new DataColumn("AccountId",typeof(int)), 
        new DataColumn("AccountName",typeof(string)), 
        new DataColumn("Password",typeof(string)),
        new DataColumn("ClassRoomId",typeof(Guid)),
        new DataColumn("AddDate",typeof(DateTime)),
        new DataColumn("IsActive",typeof(int))
      });//數(shù)據(jù)庫(kù)表結(jié)構(gòu)
      return dt;
    }
    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}

希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論