我想将bulkCopy用于存储过程,然后使用merge to upsert。
这是我的存储过程
@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
如何使用bulkCopy传递表并执行此存储过程? 在c#代码中,我想将Datatable作为参数传递,然后执行此存储过程。
答案 0 :(得分:0)
使用Table-value参数存储过程并使用C#代码将Datatable传递给存储过程。
// 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;