我创建了一个过程,该过程根据输入文本返回问题的ID
ALTER PROCEDURE [dbo].[GetQuestionIDbyTekst]
(
@Tekst nvarchar(100)
)
AS
DECLARE @QuestionID int
SELECT QuestionID
FROM dbo.Questions
WHERE Tekst = @Tekst
RETURN @QuestionID
我在获取QuestionID
的价值时遇到了问题:
public static int getQuestionID(Question p)
{
using (Entities dm = new Entities())
{
return dm.GetQuestionIDbyTekst(p.Tekst);
}
}
答案 0 :(得分:2)
将Route("Calculator/AddTwoNumbers/a/{a}/b/{a}")]
public IHttpActionResult AddTwoNumbers(PayLoad payLoad)
设为@QuestionID
参数。您还需要将结果分配给Output
@QuestionID
答案 1 :(得分:0)
请试试这个:
ALTER PROCEDURE [dbo].[GetQuestionIDbyTekst]
(
@Tekst nvarchar(100)
)
AS
-- DECLARE @QuestionID int
SELECT QuestionID
FROM dbo.Questions
WHERE Tekst = @Tekst
-- RETURN @QuestionID
答案 2 :(得分:0)
您可以使用存储过程的变体。
如果您使用ADO.NET并希望获得返回值,请尝试以下方法:
SqlConnection con = new SqlConnection(@"Data Source=localhost\***;Initial Catalog=***;Integrated Security=True;Persist Security Info=False;");
con.Open();
SqlCommand cmd = new SqlCommand("GetQuestionIDbyTekst", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@Tekst", System.Data.SqlDbType.NVarChar).Value = "eee";
SqlParameter returnPar = new SqlParameter();
returnPar.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(retturnPar);
cmd.ExecuteScalar();
var result = returnPar.Value;
如果您使用Entity Framework,则可以使用以下变体:
public static int GetQuestionIDbyTekst(string question)
{
using (var context = new EfDbContext())
{
var test = new SqlParameter("@Tekst", question);
var resultParam = new SqlParameter("@result", SqlDbType.Int);
resultParam.Direction = ParameterDirection.Output;
context.Database.ExecuteSqlCommand("exec @result = [dbo].[testProc] @Tekst", resultParam, test);
return (int)resultParam.Value;
}
}