[英]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.