[英]How to call stored procedure from C#, and get the returned value?
I created a stored procedure in SQL Server, and loaded into a.dbml
, a database file in my Visual Studio project.我在 SQL Server 中创建了一个存储过程,并加载到a.dbml
,这是我的 Visual Studio 项目中的一个数据库文件。 The procedure is called storedprocedure1(System.String name, System.Int32 code)
.该过程称为storedprocedure1(System.String name, System.Int32 code)
。
Now in my MVC controller (let's call it FooController
), I am writing a method to get the result from the stored procedure.现在在我的 MVC 控制器中(我们称之为FooController
),我正在编写一个方法来从存储过程中获取结果。 How do I do that?我怎么做?
Database class:数据库类:
namespace App.Repo.App_Data{
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="Application")]
public partial class APPDatacontext : System.Data.Linq.DataContext{ /* ... */}
}
Controller class控制器类
namespace App.Controllers{
public class FooController : Controller{
private string getResult(string name, int code){
// call and get result from storedprocedure1
}
}
}
SQL Server数据库服务器
ALTER PROCEDURE storedprocedure1 (
@name varchar(10),
@code int
@result varchar(50) OUTPUT
)
AS
SELECT --blah blah blah
GO
You don't use RETURN to return values from a procedure.您不使用 RETURN 从过程中返回值。 The RETURN value is an integer and is designed to provide a value indicating the status of the execution. RETURN 值是一个整数,旨在提供一个指示执行状态的值。 If you want values back from your procedures you should use OUTPUT variables.如果你想从你的过程中取回值,你应该使用 OUTPUT 变量。
https://msdn.microsoft.com/en-us/library/ms378108%28v=sql.110%29.aspx https://msdn.microsoft.com/en-us/library/ms378108%28v=sql.110%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.