Web API调用存储过程并返回结果

trx*_*trx 3 c# asp.net asp.net-mvc asp.net-web-api

我正在创建一个接受两个称为ACC的输入参数的Web API。创建一个存储过程以在SQL Server中插入或更新Account表。帐户表只有两个字段AccountID nvarchar(50)(primaryKey)和Cnt int

 CREATE PROCEDURE [dbo].[usp_InserUpadte] 
 @Account_TT AS Account_TT READONLY

 AS
 BEGIN
 SET NOCOUNT ON;

 BEGIN TRANSACTION;

 MERGE dbo.[Account] prj
 USING @Account_TT tt
 ON prj.AccountID = tt.AccountID
 WHEN MATCHED THEN UPDATE SET prj.Cnt = prj.Cnt+1
 WHEN NOT MATCHED THEN INSERT (AccountID,Cnt)
 VALUES (tt.AccountID, 1);

 COMMIT TRANSACTION;
Run Code Online (Sandbox Code Playgroud)

现在,我尝试连接到SQL Server,但不确定如何将存储过程调用到ASP.NET Web API应用程序中并在其中传递帐户ID来创建或更新表

namespace WebService.Controllers
{
public class CreationController : ApiController
{
    [HttpGet]
    public HttpResponseMessage Get(string ACC)
    {
        string strcon =  System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        SqlConnection DbConnection = new SqlConnection(strcon);
Run Code Online (Sandbox Code Playgroud)

我知道我们可以像这样直接调用查询

var strQuery = "SELECT * from ACCOUNT where ACC = :ACC"
Run Code Online (Sandbox Code Playgroud)

但是不知道如何调用上述存储过程并传递帐户值。任何帮助是极大的赞赏。

Saa*_*adi 5

这是完整的工作示例。请看一下。

string strcon = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);
DbConnection.Open();
SqlCommand command = new SqlCommand("[dbo].[usp_InserUpadte]", DbConnection);
command.CommandType = CommandType.StoredProcedure;

//create type table
DataTable table = new DataTable();
table.Columns.Add("AccountID", typeof(string));
table.Rows.Add(ACC);

SqlParameter parameter = command.Parameters.AddWithValue("@Account_TT", table);
parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "Account_TT";
command.ExecuteNonQuery();
DbConnection.Close();
Run Code Online (Sandbox Code Playgroud)