我有一个带参数的存储过程,我想在一个返回列表索引的视图中提交参数。我该如何在控制器中解决这个问题
CREATE PROCEDURE [dbo].[spFlugReport]
(
@AccNo INTEGER,
@DateFrom DATE,
@DateTo DATE
)
AS
BEGIN
SELECT *
FROM [dbo].[KIRData]
WHERE AccNo = @AccNo
AND StartDate >= @DateFrom
AND EndDate <= @DateTo
AND Prod = 'Air'
END
C#代码:
public ActionResult Report()
{
using(DataModel db = new DataModel())
{
SqlParameter[] param = new SqlParameter[] {
new SqlParameter("@AccNo ,"),
new SqlParameter("@DateFrom ,"),
new SqlParameter("@DateTo ,")
};
}
}
答案 0 :(得分:0)
欢迎堆栈溢出。这是一个有用的链接,可以帮助您实现所需的工作。
https://csharp-station.com/Tutorial/AdoDotNet/Lesson07
这是与您的问题How to execute a stored procedure within C# program
类似的问题但是,这是一个简单的示例,说明了将参数传递给存储过程的必要条件。
// create and open a connection object
SqlConnection conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
conn.Open();
// 1. create a command object identifying the stored procedure
SqlCommand cmd = new SqlCommand("CustOrderHist", conn);
// 2. set the command object so it knows to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@CustomerID", custId));
// execute the command
SqlDataReader rdr = cmd.ExecuteReader();
希望这对您有所帮助。
答案 1 :(得分:0)
我相信您正在寻找类似的东西。如果没有,您能否提供更多细节。
DataTable database = new DataTable();
string dbString = ConfigurationManager.ConnectionStrings["YourConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(dbString))
using (SqlCommand cmd = new SqlCommand("dbo.spFlugReport", con))
{
using(DataModel db = new DataModel())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@AccNo", AccNo);
cmd.Parameters.AddWithValue("@DateFrom", DateFrom);
cmd.Parameters.AddWithValue("@DateTo", DateTo);
con.Open();
cmd.ExecuteNonQuery();
}
}
此链接是您如何为YourConnection
:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string