我在创建用户时遇到了麻烦。当我创建一个新用户时,我的proc返回-1,但它必须返回用户id。
存储过程运行良好,它将数据插入表中,但返回-1
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_CreateUser]
@Address nvarchar(300),
@BirthDay date,
@Email nvarchar(64),
@FullName nvarchar(300),
@Image nvarchar(2000),
@Password nvarchar(512),
@PhoneNumber nvarchar(10),
@Sex nvarchar(3),
@RoleId int
AS
BEGIN TRY
SET NOCOUNT ON;
INSERT INTO [dbo].[Users] ([Address], [BirthDay], [Email], [FullName], [Image], [Password], [PhoneNumber], [Sex], [RoleId])
VALUES (@Address, @BirthDay, @Email, @FullName, @Image, @Password, @PhoneNumber, @Sex, @RoleId)
RETURN 0
END TRY
BEGIN CATCH
RETURN -1
END CATCH
我的存储库方法创建:
public async Task<int> Create(User user)
{
string sql = $"sp_CreateUser @BirthDay = '{user.BirthDay.ToString("yyyy-MM-dd")}', @Email = '{user.Email}', @FullName = '{user.FullName}', @Password = '{user.Password}', @RoleId = {user.RoleId}, @Address = '{user.Address}', @Image = '{user.Image}', @PhoneNumber = '{user.PhoneNumber}', @Sex = '{user.Sex}'";
int result = await _db.Database.ExecuteSqlCommandAsync(sql);
return result;
}
答案 0 :(得分:0)
在Try / Catch中尝试使用以下查询。
Dim x, x2, y, y2()
Dim i As Long
Dim dict As Object
Dim LastRowForDict As Long
Dim p As Long
dim ws as worksheet
dim LastRowResult as long
set ws = worksheets("DictionaryTest")
Set dict = CreateObject("Scripting.Dictionary")
With ws
LastRowForDict = .Range("B" & rows.Count).End(xlUp).Row
For p = 1 To LastRowForDict
If ws.Range("N" & p).Value = "ABC" Then 'only adds to dictionary if line is an "ABC" line
x = .Range("H2:H" & LastRowForDict).Value
x2 = .Range("AG2:AG" & LastRowForDict).Value
'Check if key exists and if yes add new value to existing item (SUM them)
''' For i = 1 To UBound(x, 1) should this be here?
If Not dict.Exists(x(p, 1)) Then
dict.Item(x(p, 1)) = x2(p, 1)
Else
dict.Item(x(p, 1)) = CDbl(dict.Item(x(p, 1))) + CDbl(x2(p, 1))
End If
'''next i should this be here?
End If
Next p
End With
'map the values
With ws
LastRowResult = .Range("B" & rows.Count).End(xlUp).Row
y = .Range("H2:H" & LastRowResult).Value 'looks up to this range
ReDim y2(1 To UBound(y, 1), 1 To 1) '<< size the output array
For i = 1 To UBound(y, 1)
If dict.Exists(y(i, 1)) Then
y2(i, 1) = dict(y(i, 1))
Else
y2(i, 1) = ""
End If
Next i
.Range("CK2:CK" & LastRowResult).Value = y2 '<< place the output on the sheet
End With
它将为您提供有关触发执行catch块的内容的更多信息
有关此检索错误信息的更多信息,请访问此处。
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/try-catch-transact-sql
答案 1 :(得分:0)
<强>解决!强>
我已经使用SqlParameter添加了返回参数,现在一切运行良好。
public async Task<int> Create(User user)
{
var param = new SqlParameter
{
ParameterName = "@CreatedId",
SqlDbType = SqlDbType.Int,
Direction = ParameterDirection.Output
};
string sql = $"exec @CreatedId = sp_CreateUser @BirthDay = '{user.BirthDay.ToString("yyyy-MM-dd")}', @Email = '{user.Email}', @FullName = '{user.FullName}', @Password = '{user.Password}', @RoleId = {user.RoleId}, @Address = '{user.Address}', @Image = '{user.Image}', @PhoneNumber = '{user.PhoneNumber}', @Sex = '{user.Sex}'";
int result = await _db.Database.ExecuteSqlCommandAsync(sql, param);
return (int)param.Value;
}
或另一种方法是在存储过程中设置
SET NOCOUNT OFF