使用存储过程将对象列表保存到单独的表

时间:2017-08-09 06:14:28

标签: c# sql-server asp.net-mvc stored-procedures

我有一个包含对象列表的类,如下所示。

public class Details
    {

        public string Reference { get; set; }

        public List<Hotel> HotelDetails { get; set; }
 }

我已经创建了存储过程来保存Details表值。并且它的工作正常。以下是我的存储过程。

USE [UL_SLHEV]
GO
/****** Object:  StoredProcedure [dbo].[SP_EVoucherDetails_Insert]    Script Date: 8/9/2017 11:20:31 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_Details_Insert]
    @XRequestReference varchar(15),
AS
BEGIN

    DECLARE @RequestReference AS VARCHAR(15) = ISNULL(@XRequestReference,'')


    INSERT INTO Voucher(
    RequestReference,
    )

    VALUES(
    @RequestReference,
    )

END

在这里,我必须在同一个保存方法和相同的存储过程中将酒店详细信息列表保存在另一个表中。以下是我用来保存表中酒店详细信息的C#代码。

public EVoucherDetails SaveEVoucherDetails(EVoucherDetails detailsModel)
        {
            Func<SqlCommand, EVoucherDetails> injector = (cmd) =>
            {
                cmd.Parameters.Add("@XRequestReference", SqlDbType.VarChar).Value = detailsModel.RequestReference;

                var pList = new SqlParameter("@XHotelInfoDetails", SqlDbType.Structured);
                pList.TypeName = "dbo.HotelInfo";
                pList.Value = GetHotelList(detailsModel.HotelInfo, detailsModel.RequestReference);
                cmd.Parameters.Add(pList);

            };
            return _sqlHelper.SqlSpExecute("SP_Details_Insert", injector);
        }

 private List<SqlDataRecord> GetHotelList(List<HotelInfo> list, string RequestRef)
{
    List<SqlDataRecord> datatable = new List<SqlDataRecord>();

    SqlMetaData[] sqlMetaData = new SqlMetaData[11];
    sqlMetaData[0] = new SqlMetaData("HotelID", SqlDbType.Int);
    sqlMetaData[1] = new SqlMetaData("RequestReference", SqlDbType.VarChar, 15);
    sqlMetaData[2] = new SqlMetaData("FromDate", SqlDbType.DateTime);
    sqlMetaData[3] = new SqlMetaData("ToDate", SqlDbType.DateTime);
    sqlMetaData[4] = new SqlMetaData("Nights", SqlDbType.Int);
    sqlMetaData[5] = new SqlMetaData("MealPlan", SqlDbType.VarChar, 10);
    sqlMetaData[6] = new SqlMetaData("StarCategory", SqlDbType.Int);
    sqlMetaData[7] = new SqlMetaData("Status", SqlDbType.Int);
    sqlMetaData[8] = new SqlMetaData("LastUpdatedBy", SqlDbType.VarChar, 50);
    sqlMetaData[9] = new SqlMetaData("LastUpdatedDate", SqlDbType.DateTime);
    sqlMetaData[10] = new SqlMetaData("CreatedDateTime", SqlDbType.DateTime);
    sqlMetaData[10] = new SqlMetaData("CreatedUserId", SqlDbType.VarChar, 15);

    foreach (var info in list) {
        SqlDataRecord row = new SqlDataRecord(sqlMetaData);
        int? hotelId = GetHoteId(info.HotelName);
        row.SetValues(hotelId, RequestRef, info.FromDate, info.ToDate, info.Nights, info.MealPlan, info.StarCategory, info.Status, info.LastUpdatedBy, info.LastUpdatedDateTime, info.CreatedDateTime, info.CreatedUserId);
        datatable.Add(row);
    }

    return datatable;
}

我不喜欢&#39;知道如何通过使用上述相同的存储过程来获得酒店详细信息。有人可以帮帮我吗?

由于

1 个答案:

答案 0 :(得分:0)

在sql中使用参数时,它看起来像:

  exec SP_Details_Insert "InputValueHere"

您有多个字段:

 sqlMetaData[0] = new SqlMetaData("HotelID", SqlDbType.Int);
    sqlMetaData[1] = new SqlMetaData("RequestReference", SqlDbType.VarChar, 15);
    sqlMetaData[2] = new SqlMetaData("FromDate", SqlDbType.DateTime);
    sqlMetaData[3] = new SqlMetaData("ToDate", SqlDbType.DateTime);
    sqlMetaData[4] = new SqlMetaData("Nights", SqlDbType.Int);
    sqlMetaData[5] = new SqlMetaData("MealPlan", SqlDbType.VarChar, 10);
    sqlMetaData[6] = new SqlMetaData("StarCategory", SqlDbType.Int);
    sqlMetaData[7] = new SqlMetaData("Status", SqlDbType.Int);
    sqlMetaData[8] = new SqlMetaData("LastUpdatedBy", SqlDbType.VarChar, 50);
    sqlMetaData[9] = new SqlMetaData("LastUpdatedDate", SqlDbType.DateTime);
    sqlMetaData[10] = new SqlMetaData("CreatedDateTime", SqlDbType.DateTime);
    sqlMetaData[10] = new SqlMetaData("CreatedUserId", SqlDbType.VarChar, 15);

现在,如果您想要Insert所有此记录,则Stored Procedure无法处理此数据,因为它只使用一个参数值VALUES( @RequestReference, )

您的SP需要多个参数与数据相同,并循环data<List>以一次插入一个参数。