繁体   English   中英

ASP.NET和SQL将类传递给存储过程?

[英]ASP.NET and SQL pass a class to a stored procedure?

我有一个像这样的ASP.NET MVC 4类:

public class CellModel
    {
        public uint scheduleTaskID { get; set; }
        public string task { get; set; }
        public string baselineDate { get; set; }
        public string scheduledDate { get; set; }
        public string actualDate { get; set; }
        public string finishedDate { get; set; }
        public string expectedStart { get; set; }
        public string expectedFinish { get; set; }
        public string plusMinus { get; set; }
        public string completedBy { get; set; }
        public bool selected { get; set; }
        public bool isEditableRow { get; set; }
        public uint sortOrder { get; set; }

        public override string ToString()
        {
            return scheduleTaskID.ToString();
        }
    }

并且我有一个SQL存储过程,如下所示:

USE [database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[PostScheduledTasks] 
    -- Add the parameters for the stored procedure here
    @actualStart datetime = NULL, 
    @actualFinish datetime = NULL,
    @actualEndDate datetime = NULL,
    @UserDate1 datetime = NULL,
    @IsCompleted bit = 0,
    @PercentComplete float = 0.0,
    @UStmp varchar(10) = NULL,
    @SchedukeTaskID int = 0,
    @SortOrder int = 0
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE ScheduleTasks  
                      SET 
                          ActualStart=@actualStart,
                          ActualFinish=@actualFinish,
                          ActualEndDate=@actualEndDate,
                          UserDate1=@UserDate1,
                          IsCompleted=@IsCompleted,
                          PercentComplete=@percentComplete,
                          UStmp = @UStmp
                      WHERE ScheduleTaskID = @SchedukeTaskID
                      AND SortOrder = @SortOrder
END

现在我的单元格可能有多个项目,我的问题是我应该在运行存储过程的.NET中循环遍历该类吗? 还是将整个类传递给存储过程并为该类中的每个项目运行更新?

这是我通过.NET开始的内容:

public UserModel PostScheduledTasks(List<CellModel> cells)
        {

            try
            {
                using (connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    using (SqlCommand command = new SqlCommand("PostScheduledTasks", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;

                        /*SqlParameter parameter1 = new SqlParameter("@jobNoPO", SqlDbType.VarChar);
                        parameter1.Value = jobNoPO;
                        parameter1.Direction = ParameterDirection.Input;
                        command.Parameters.Add(parameter1);*/

                        command.ExecuteNonQuery();

                        UserModel userModel = new UserModel();
                        userModel.name = "true";
                        userModel.userName = "true";
                        return userModel;
                    }
                }
            }
            catch
            {
                UserModel nullModel = new UserModel();
                nullModel.name = "NFD";
                nullModel.userName = "NFD";
                return nullModel;
            }
            finally
            {
                connection.Close();
            }
        }

无法将类传递给SQL Server。 他们是两个不同的平台。 有一些工具可以使SQL Tables看起来像类(例如Entity Framework)。您还可以使用一些工具,使您可以与在运行时创建的类和对象与SQL进行交互。 实际上,我写了一篇。 它非常简单,仅包含一个文件。

https://gist.github.com/hoganlong/b7f5c5e8dde61ae3cd6f

这是如何使用它的示例。

SuperSimple.DB.ExecuteNonQuery("spName", 
                               "connectionStr", 
                               CommandType.StoredProcedure, 
                               new { p1 = "a", p2 = "b" });

这将基于动态类调用带有两个参数p1和p2的spName。

如果你做了

CellModel test = new CellModel() // etc
SuperSimple.DB.ExecuteNonQuery("PostScheduledTasks", 
                               "connectionStr", 
                               CommandType.StoredProcedure, 
                               test );

这将失败,因为PostScheduledTasks没有CellModel的所有字段的参数。

您可以修改存储过程以接受所有字段。

或者,您可以修改我的代码以不传递所有字段。 例如,您可以使用元数据装饰器来标记要传递的字段。 其他更复杂的工具也可以做到这一点-我的工具旨在尽可能简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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