我正在寻找使用 c# 将 xml 文件作为存储过程参数传递的最简单方法。实际上,我需要创建一个 xml 文件,以便将此 xml 保存到 sql 数据库中。在数据库存储过程中有一个参数为xml 类型。我已经创建了一个如下所述的 xml 文件
StringWriter stringWriter = null;
XmlTextWriter writer = null;
try
{
stringWriter = new StringWriter(new StringBuilder());
writer = new XmlTextWriter(stringWriter);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteComment("Holiday Info");
writer.WriteStartElement("holidayInfo", "");
for (int groupIndex = 0; groupIndex < numberOfGroup; groupIndex++)
{
for (int religionIndex = 0; religionIndex < numberOfReligion; religionIndex++)
{
for (int jobStationIndex = 0; jobStationIndex < numberOfJobStation; jobStationIndex++)
{
writer.WriteStartElement("HOLIDAY", "");
writer.WriteAttributeString("intGroupID", chkGroupList.Items[groupIndex].Value.ToString());
writer.WriteAttributeString("intJobTypeId", "0");
writer.WriteAttributeString("intJobStationID", chkReligionList.Items[religionIndex].Value.ToString());
writer.WriteAttributeString("intHolidayID", ddlHolidayName.SelectedValue.ToString());
writer.WriteAttributeString("dtePermitedDate", DateTime.Now.ToShortDateString());
writer.WriteAttributeString("intReligionId", chkReligionList.Items[religionIndex].Value.ToString());
writer.WriteAttributeString("dteFromDate", txtFromDate.Text);
writer.WriteAttributeString("dteToDate", txtToDate.Text);
writer.WriteEndElement();
}
}
}
stringWriter.GetStringBuilder().ToString();
return will be what if i want to sent this file to the store procedure parameter?;
}
finally
{
if (writer != null) writer.Close();
if (stringWriter != null) stringWriter.Close();
}
如果我想将此文件作为 xml 参数发送到存储过程,返回类型将是什么?
这是我的商店程序
CREATE PROCEDURE [dbo].[sprHoliday_InsertHolidaysGroupPermission]
@intUserID INT =NULL,
@xmlHolidayPermissionDetails XML,
@insertStatus AS VARCHAR(200) OUT
AS
BEGIN
DECLARE @intEmployeeID int
IF (@intUserID IS NOT NULL)
BEGIN
SELECT @intEmployeeID = intEmployeeID From dbo.tblUserInfo WHERE tblUserInfo.intUserID = @intUserID
END
BEGIN TRANSACTION
BEGIN TRY
INSERT INTO dbo.tblEmployeeGroupPermissionHolidays
(intGroupID,
intJobTypeId,
intJobStationID,
intHolidayID,
dtePermitedDate,
intReligionId,
dteFromDate,
dteToDate)
(SELECT xmlHolidayPermissionDetails.item.value('@intGroupID[1]', 'INT'),
xmlHolidayPermissionDetails.item.value('@intJobTypeId[1]', 'INT'),
xmlHolidayPermissionDetails.item.value('@intJobStationID[1]', 'INT'),
xmlHolidayPermissionDetails.item.value('@intHolidayID[1]', 'INT'),
xmlHolidayPermissionDetails.item.value('@dtePermitedDate[1]', 'DATE'),
xmlHolidayPermissionDetails.item.value('@intReligionId[1]', 'INT'),
xmlHolidayPermissionDetails.item.value('@dteFromDate[1]', 'DATE'),
xmlHolidayPermissionDetails.item.value('@dteToDate[1]', 'DATE')
FROM @xmlHolidayPermissionDetails.nodes('//HOLIDAY') AS xmlHolidayPermissionDetails(item)
)
COMMIT
INSERT INTO dbo.tblHRDataHistory values('Insert','Holiday Permission Data Insert','tblEmployeeGroupPermissionHolidays',GETDATE(),@intEmployeeID)
SET @insertStatus = 'Holiday Permission has been iserted succesfully'
END TRY
BEGIN CATCH
Rollback
SELECT @insertStatus = 'There was an error! ' + ERROR_MESSAGE()
END CATCH
END