简体   繁体   English

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

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

I have an ASP.NET MVC 4 class like so: 我有一个像这样的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();
        }
    }

and I have an SQL stored procedure like so: 并且我有一个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

Now my cell may have multiple items, my question is should I loop through the class in .NET running the stored procedure how ever many times there are items in the class. 现在我的单元格可能有多个项目,我的问题是我应该在运行存储过程的.NET中循环遍历该类吗? Or is there away to pass the whole class to the stored procedure and run the update for each item in the class? 还是将整个类传递给存储过程并为该类中的每个项目运行更新?

Here is what I have started with via .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();
            }
        }

There is no way to pass a class to SQL Server. 无法将类传递给SQL Server。 They are two different platforms. 他们是两个不同的平台。 There are some tools which will make SQL Tables seem like classes (eg Entity Framework) You can also have tools that allow you to interact with SQL with classes and objects created at run-time. 有一些工具可以使SQL Tables看起来像类(例如Entity Framework)。您还可以使用一些工具,使您可以与在运行时创建的类和对象与SQL进行交互。 In fact, I wrote one. 实际上,我写了一篇。 It is very simple and consists of only one file. 它非常简单,仅包含一个文件。

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

Here is an example of how it could be used. 这是如何使用它的示例。

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

This will call spName with two parameters p1 and p2 based on dynamic class. 这将基于动态类调用带有两个参数p1和p2的spName。

If you did 如果你做了

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

It would fail because PostScheduledTasks does not have parameters for all fields of CellModel. 这将失败,因为PostScheduledTasks没有CellModel的所有字段的参数。

You could modify your stored procedure to accept all fields. 您可以修改存储过程以接受所有字段。

Or you could modify my code to not pass all fields. 或者,您可以修改我的代码以不传递所有字段。 For example you could use meta data decorators to mark which fields to pass. 例如,您可以使用元数据装饰器来标记要传递的字段。 Other more complicated tools do stuff like this -- my tool is intended to be as simple as possible. 其他更复杂的工具也可以做到这一点-我的工具旨在尽可能简单。

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

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