gpt4 book ai didi

c#-4.0 - C#批量插入SQLBulkCopy-如果存在则更新

转载 作者:行者123 更新时间:2023-12-03 13:29:35 25 4
gpt4 key购买 nike

这个问题已经在这里有了答案:




已关闭8年。




Possible Duplicate:
Any way to SQLBulkCopy “insert or update if exists”?



我正在使用 SQLBulkCopy插入批量记录

如何对已存在的记录进行更新(而不是插入)? SQLBulkCopy是否可能?

这是我的SQLBulkCopy代码
using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
{
bulkCopy.BatchSize = CustomerList.Count;
bulkCopy.DestinationTableName = "dbo.tCustomers";
bulkCopy.ColumnMappings.Clear();
bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
bulkCopy.ColumnMappings.Add("LastName", "LastName");
bulkCopy.ColumnMappings.Add("Address1", "Address1");
bulkCopy.ColumnMappings.Add("Address2", "Address2");
bulkCopy.WriteToServer(CustomerList);
}

申请详情
  • ASP.net MVC 3.0 Razor View 引擎
  • SQL Server 2008的
  • 最佳答案

    感谢@pst

    带着他的建议,如果有人必须实现类似的方法,这就是我的实现方法。

    批量插入永久温度表

     using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
    {
    bulkCopy.BatchSize = CustomerList.Count;
    bulkCopy.DestinationTableName = "dbo.tPermanentTempTable";
    bulkCopy.ColumnMappings.Clear();
    bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
    bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
    bulkCopy.ColumnMappings.Add("LastName", "LastName");
    bulkCopy.ColumnMappings.Add("Address1", "Address1");
    bulkCopy.ColumnMappings.Add("Address2", "Address2");
    bulkCopy.WriteToServer(CustomerList);
    }

    然后调用存储过程以将临时表与实际表合并
     using (Entities context = new Entities())
    {
    System.Nullable<int> iReturnValue = context.usp_Customer_BulkUploadMerge(customerid, locationID).SingleOrDefault();
    if (iReturnValue.HasValue)
    {
    // return was successful!
    }
    }

    这就是我在存储过程中使用合并的方式
    ALTER PROCEDURE usp_Customer_BulkUploadMerge
    (
    @CustomerID INT ,
    @locationID INT
    )
    AS
    BEGIN
    DECLARE @retValue INT
    BEGIN TRY
    IF OBJECT_ID('tCustomers') IS NOT NULL
    BEGIN
    BEGIN TRANSACTION MergPatientsTable
    SET NOCOUNT ON;
    MERGE dbo.tCustomers AS target
    USING
    ( SELECT PU.CustomerID ,
    PU.LocationID ,
    PU.FirstName ,
    PU.LastName ,
    PU.MiddleInitial ,
    PU.Gender ,
    PU.DOB

    FROM dbo.tPermanentTempTable PU
    WHERE PU.CustomerID = @CustomerID
    AND PU.LocationID = @locationID
    GROUP BY PU.CustomerID ,
    PU.LocationID ,
    PU.FirstName ,
    PU.LastName ,
    PU.MiddleInitial ,
    PU.Gender ,
    PU.DOB

    ) AS source ( CustomerID, LocationID, FirstName,
    LastName, MiddleInitial, Gender, DOB )
    ON ( LOWER(target.FirstName) = LOWER(source.FirstName)
    AND LOWER(target.LastName) = LOWER(source.LastName)
    AND target.DOB = source.DOB
    )
    WHEN MATCHED
    THEN
    UPDATE SET
    MiddleInitial = source.MiddleInitial ,
    Gender = source.Gender,
    LastActive = GETDATE()
    WHEN NOT MATCHED
    THEN
    INSERT (
    CustomerID ,
    LocationID ,
    FirstName ,
    LastName ,
    MiddleInitial ,
    Gender ,
    DOB ,
    DateEntered ,
    LastActive
    ) VALUES
    ( source.CustomerID ,
    source.LocationID ,
    source.FirstName ,
    source.LastName ,
    source.MiddleInitial ,
    source.Gender ,
    source.DOB ,
    GETDATE() ,
    NULL
    );
    DELETE PU
    FROM dbo.tPermanentTempTable PU
    WHERE PU.CustomerID = @CustomerID
    AND PU.LocationID = @locationID
    COMMIT TRANSACTION MergPatientsTable
    SET @retValue = 1
    SELECT @retValue
    END
    ELSE
    BEGIN
    SET @retValue = -1
    SELECT @retValue
    END
    END TRY
    BEGIN CATCH
    ROLLBACK TRANSACTION MergPatientsTable
    DECLARE @ErrorMsg VARCHAR(MAX);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;
    SET @ErrorMsg = ERROR_MESSAGE();
    SET @ErrorSeverity = ERROR_SEVERITY();
    SET @ErrorState = ERROR_STATE();
    SET @retValue = 0
    SELECT @retValue
    -- SELECT 0 AS isSuccess
    END CATCH
    END

    关于c#-4.0 - C#批量插入SQLBulkCopy-如果存在则更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12521692/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com