首先我创建了一个调用存储过程的方法,我向该方法传递了sections(sections table entity) 和客户 ID 的列表
public bool SaveChatSectionUserMapping(List<Sections> lstSections, int customerId)
{
con = new SqlConnection(connectionString);
bool isUpdated = false;
try
{
string xmlString = string.Empty;
xmlString = XMLOperations.WriteXML(lstSections);
SqlCommand cmd = new SqlCommand("spUpdateSections", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@XMLData", SqlDbType.Xml).Value = xmlString;
cmd.Parameters.Add("@CustomerId", SqlDbType.Int).Value = customerId;
SqlParameter param = new SqlParameter();
param.SqlDbType = SqlDbType.Bit;
param.Direction = ParameterDirection.Output;
param.ParameterName = "@Result";
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
isUpdated = (param.Value != DBNull.Value) ? Convert.ToBoolean(param.Value) : false;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return isUpdated;
}
我从xmlString = XMLOperations.WriteXML(lstSections);这一行得到的xmlString的值是这样的
<ArrayOfSection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Sections>
<UserId>1</UserId>
<SectionId>1</SectionId>
<IsActive>true</IsActive>
</Sections>
<Sections>
<UserId>1</UserId>
<SectionId>2</SectionId>
<IsActive>true</IsActive>
</Sections>
<Sections>
<UserId>1</UserId>
<SectionId>5</SectionId>
<IsActive>true</IsActive>
</Sections>
</ArrayOfSection>
现在在存储过程中
CREATE Procedure [dbo].[spUpdateSections]
(
@XMLData as XML,
@CustomerId INT,
@Result int Output
)
AS
BEGIN
SET NOCOUNT ON;
Declare @ErrorCode Varchar(100) = '',@propertyCount VArchar(100) = '',@currentCount int=1,@SectionId int, @IsActive bit
Begin TRY
UPDATE Sections
SET
IsActive = 0
WHERE
UserId = @CustomerId
SELECT @propertyCount = convert(VARCHAR, @XMLData.query ('count(/ArrayOfSection/Sections)'))
SET @currentCount = 1
while (@currentCount<=@propertyCount)
Begin
SET @SectionId = @XMLData.value('data(/ArrayOfSection/Sections[sql:variable("@currentCount")]/SectionId)[1]', 'INT')
SET @IsActive = @XMLData.value('data(/ArrayOfSection/Sections[sql:variable("@currentCount")]/IsActive)[1]', 'BIT')
If Exists (SELECT *
FROM
Sections
WHERE
UserId = @CustomerId
AND SectionId = @SectionId)
BEGIN
IF(@IsActive=1)
BEGIN
UPDATE Sections
SET
IsActive = 1
WHERE
UserId = @CustomerId AND
SectionId = @SectionId
END
END
ELSE
BEGIN
IF(@IsActive=1)
BEGIN
INSERT INTO Sections
([SectionId]
,[UserId]
,[IsActive])
VALUES
(@SectionId,@CustomerId,1)
END
END
SET @currentCount = @currentCount + 1
End
SET @Result = 1
ErrorCode:
If(@ErrorCode !='')
BEGIN
--SELECT @BSErrorResult = doctor.GetErrorCodeDetail(@ErrorCode)
SET @Result = 2
END
END TRY
BEGIN CATCH
--Declaring Variable for formating error
Declare @ErrorMessage VARCHAR(max),
@ErrorSeverity INT,
@ErrorState INT
--SELECTING TECHNICAL ERROR
SELECT @ErrorMessage = error_message()
, @ErrorSeverity = error_severity()
, @ErrorState = error_state()
, @ErrorMessage = @ErrorMessage + ' ' + db_name() + ' ' + ' ' + object_name(@@PROCID);
RAISERROR (
@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
End CATCH
END
另一种生成 XMl 的方法是像这样使用 XElement
XElement xml = new XElement("Sections",
from col in lstSections
select new XElement("rows",
new XElement("UserId", col.UserId),
new XElement("SectionId", col.SectionId),
new XElement("IsActive", col.IsActive)));
string xmlString = xml.ToString();