SqlBulkCopy大数据复制插入数据_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > SqlBulkCopy大数据复制插入数据

SqlBulkCopy大数据复制插入数据

 2015/4/10 19:01:45  zheyiw  程序员俱乐部  我要评论(0)
  • 摘要:publicpartialclassASP_NET:System.Web.UI.Page{privateDateTimestartTime;stringconnString="";//数据库连接protectedvoidButton1_Click(objectsender,EventArgse){startTime=DateTime.Now;SqlConnectionsrcConnection=newSqlConnection();srcConnection
  • 标签:复制 数据 SQL
class="java" name="code">

public partial class ASP_NET : System.Web.UI.Page
{
    private DateTime startTime;
	string connString = "";//数据库连接

    protected void Button1_Click(object sender, EventArgs e)
    {
        startTime = DateTime.Now;
        SqlConnection srcConnection = new SqlConnection();
		srcConnection.ConnectionString = connString;
		
        SqlCommand sqlcmd = new SqlCommand();                   
        sqlcmd.Connection = srcConnection;
        sqlcmd.CommandText = "select * from abc"; //将这个表的数据复制到"bcd"表中
        sqlcmd.CommandType = CommandType.Text;
        sqlcmd.Connection.Open();
		
		DataTable dt = new DataTable(); 
		SqlDataAdapter da = new SqlDataAdapter();   
        da.SelectCommand = sqlcmd;	 
        da.Fill(dt);

        SqlBulkCopy sbc = new SqlBulkCopy(connString,SqlBulkCopyOptions.UseInternalTransaction); //支持事物
        sbc.BulkCopyTimeout = 5000;
        sbc.SqlRowsCopied +=new SqlRowsCopiedEventHandler(OnRowsCopied);
        sbc.NotifyAfter = dt.Rows.Count;
        try
        {
            sbc.DestinationTableName = "bcd";
            sbc.WriteToServer(dt); //开始复制数据
        }
        catch (Exception ex)
        {
            lblCounter.Text = ex.Message.ToString();
        }
        finally
        {
            sqlcmd.Clone();
            srcConnection.Close();
        }
    }
	
	//复制完成,响应事件
    private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
    {
        lblCounter.Text += args.RowsCopied.ToString() + " rows are copied<Br>";
        TimeSpan copyTime = DateTime.Now - startTime;
        lblCounter.Text += "Copy Time:" + copyTime.Seconds.ToString() + "." + copyTime.Milliseconds.ToString() + " seconds";
    }
}

上一篇: [转]Java中的堆和栈 下一篇: 没有下一篇了!
发表评论
用户名: 匿名