• ADO.NET


    方法一:使用SqlBulkCopy实现批量更新或添加数据。

     SqlBulkCopy类一般只能用来将数据批量插入打数据库中,如果数据表中设置了主键,出现重复数据的话会报错,如果没有设置主键,那么将会添加同样的数据,导致数据重复。这里有两种方案可实行方案,实现批量将数据更新到数据表中。如果数据存在,就更新数据;如果不存在,则添加一条新的数据

    这两种方案都需要添加一个新的用作临时存储数据的表,假如有两个表 BatchTableTemp和BatchTable, SqlBulkCopy类先批量将数据添加到BatchTableTemp中,然后再将BatchTable表中不存在的数据添加到BatchTable表中,存在的数据更新到BatchTable表中

    方案一:在执行完SqlBulkCopy批量将数据添加到BatchTableTemp以后,执行存储过程,BatchTable表中不存在的数据就添加Insert,存在的数据更新Update

    SqlHelper代码:

    //<summary>
    //返回操作影响的行数
    //</summary>
    public static int ExcuteNonQuery( CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
    {
        SqlCommand cmd = PrepareSqlCommand(cmdType, cmdText, cmdParms);
        int value = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        cmd.Connection.Close();
        return value;
    }
    
    /// <summary>
    /// 批量添加数据
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static bool ExcuteNonQuery(DataTable dt)
    {
        SqlConnection connection = new SqlConnection(connString);
        connection.Open();
        SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connection);
        sqlbulkcopy.BulkCopyTimeout = 100;  //超时之前操作完成所允许的秒数
        sqlbulkcopy.BatchSize = dt.Rows.Count;  //每一批次中的行数
        sqlbulkcopy.DestinationTableName = dt.TableName;  //服务器上目标表的名称
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            sqlbulkcopy.ColumnMappings.Add(i, i);  //映射定义数据源中的列和目标表中的列之间的关系
        }
        sqlbulkcopy.WriteToServer(dt);  // 将DataTable数据上传到数据表中
        connection.Close();
        return true;
    }

    C#执行ExcuteNonQuery(),批量添加到BatchTableTemp,然后在执行存储过程代码

    DataTable table = GetDataTable("BatchTableTemp"); //获取要更新的DataTable
    bool value = SqlHelper.ExcuteNonQuery(table);
    if (value)
    {
        SqlHelper.ExcuteNonQuery(CommandType.StoredProcedure, "InsertOrUpdate", null);
    }

    将BatchTableTemp同步更新到BatchTable表中的存储过程代码:

    ALTER procedure [dbo].[InsertOrUpdate]
    as    
        begin
            update BatchTable   
            set BatchTable.Column1=BatchTableTemp.Column1,
                BatchTable.Column2=BatchTableTemp.Column2,
                BatchTable.Column3=BatchTableTemp.Column3,
                BatchTable.Column4=BatchTableTemp.Column4,
                BatchTable.Column5=BatchTableTemp.Column5
            from BatchTable,BatchTableTemp
            where BatchTable.Gradation=BatchTableTemp.Gradation
            
            insert into BatchTable
            select * from BatchTableTemp
            where not exists 
                (select Gradation from BatchTable where BatchTable.Gradation=BatchTableTemp.Gradation)
            delete BatchTableTemp
        end


    方案二:待续

    方法二:利用表值参数(简称TVPS)批量更新(或插入)数据

    待续

  • 相关阅读:
    Week7 作业 A
    Mouth 1 模拟题 CSP201512-3 画图
    C++出现:error: passing 'const Employee' as 'this' argument of 'int Employee::getSalary()' discards qualifiers [-fpermissive]
    C++出现error: no match for call to '(MyCompare) (const key_type&, const Person&)'
    CentOS 7出现ifconfig:command not found
    有效清理C盘内存
    重装系统之后出现https:/ / logincdn.msauth.net/shared/1.0/content/js/ConvergedLogin_PCore_xxvbETmiVPe1AsI9xwHp3A2.js
    C++/QT运行时出现void value not ignored as it ought to be
    运行QT项目文件夹下的exe文件提示找不到“各种dll”
    Ubuntu16.04配置OpenCV3.2.0
  • 原文地址:https://www.cnblogs.com/tracine0513/p/4037608.html
Copyright © 2020-2023  润新知