简体   繁体   中英

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

I have an ASP.NET MVC 4 class like so:

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:

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. 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:

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. 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. In fact, I wrote one. It is very simple and consists of only one file.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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