zoukankan      html  css  js  c++  java
  • 一些SqlBuckCopy心得

    一些SqlBuckCopy心得,项目很多地方需要用到批量insert,这个时候如果一条条插入,很影响性能,整张表格被锁在那,而SqlBuckCopy则解决了这个问题。
    1:首先,在代码中建一张中间DataTable

    DataTable dt =new DataTable();
    dt.Columns.Add(Column1,typeof(String));
    dt.Columns.Add(Column2,typeof(String));
    dt.Columns.Add(Column2,typeof(String));

    2:然后将需要批量插入的数据先放入DataTable中。

    3:写sql语句了,先判断变量表是否存在

    IF OBJECT_ID(N'#tempTable') is not null
    drop table #tempTable
    create table #tempTable
    ([Column1] [varchar] (4) not null,
    [Column2] [varchar] (4) not null,
    [Column3] [varchar] (4) not null,)

    4:执行ExecuteNonQuery

    5:使用SqlBulkCopy 

    SqlBulkCopy sbc =new SqlBulkCopy (db.conn);
    sbc.DestinationTableName="#tempTable";
    sbc.WriteToServer(dt);

    6:写insert语句将临时表中数据存入目标表中

    insert into 目标表 (Column1,Column2,Column3)
    select Column1,Column2,Column3 from #tempTable

    7:关闭连接

    附上一些sql脚本心得:创建cursor,移动cursor,到销毁cursor,sql的cursor使用

    using 数据库名
    declare @tb table(orgid varchar(4))
    insert into @tb (orgid) select distinct orgid from 表名
    declare orgids_cursor CURSOR
    FOR
          select orgid from @tb
    OPEN orgids_cursor 
    DECLARE @orgid varchar(4)
    FETCH NEXT FROM orgids_cursor into @orgid 
    WHILE (@@FETCH_STATUS<>-1)
    BEGIN
         IF(@@FETCH_STATUS<>-2)
         BEGIN
         END
         FETCH NEXT FROM orgids_cursor INTO @orgid 
    END
    CLOSE orgids_cursor 
    DEALLOCATE orgids_cursor 

    分页查询

    select row_number() over(order by column1,column2) as 'RowNum',column1,column2,
    case when year(column1)='9999' then null
    when datediff(DD,Getdate(),column1)<0 then null
    else datediff(DD,Getdate(),column1) end as 'RemainderDays'
    from TableName
  • 相关阅读:
    价值投资-买股票操作流程
    win10安装mysql8
    .NET Debugging Demos Lab 7: Memory Leak
    .NET Debugging Demos Lab 6: Debugging Challenge
    .NET Debugging Demos Lab 5: Crash
    .NET Debugging Demos Lab 3: Memory
    .NET Debugging Demos Lab 1: Hang- Walkthrough
    .NET Debugging Demos Lab 1: Hang
    【翻译 windbg-3】高级WinDbg 使用教程
    【翻译 windbg-2】Getting started with windbg
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/2968750.html
Copyright © 2011-2022 走看看