简体   繁体   中英

SqlBulkCopy to stored procedure using Type Table

I want to use a bulkCopy to stored procedure then use merge to upsert.

here is my stored procedure

@bulkLdapGroup  [TEMP_LDAP_GROUP] readonly

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

merge into [SECURITY_CUBE].[dbo].[LDAP_GROUP] as Target
using @bulkLdapGroup as Source
on Target.[GROUP_NAME]=Source.[GROUP_NAME]
when matched then 
update set Target.[DESCRIPTION]=Source.[DESCRIPTION], Target.[WHEN_CREATED] = Source.[WHEN_CREATED],Target.[UPDATED_DATE]= GetDate()
when not matched then 
insert ([GROUP_NAME],[DESCRIPTION],[WHEN_CREATED],[UPDATED_DATE]) values (Source.[GROUP_NAME],Source.[DESCRIPTION],Source.[WHEN_CREATED],GetDate());


end

How can i pass a table in using bulkCopy and execute this stored procedure? In the c# code, i want to pass Datatable as parameter, then execute this stored procedure.

Use Table-value parameter store procedure and use C# code to pass your Datatable to store procedure.


// Create a DataTable 
DataTable dt = new DataTable(...)
// Configure the SqlCommand and SqlParameter.
SqlCommand insertCommand = new SqlCommand(
    "your TVP store procedure", connection);
insertCommand.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParam = insertCommand.Parameters.AddWithValue(
    "@tvp", dt);
tvpParam.SqlDbType = SqlDbType.Structured;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM