我有這個存儲過程:如何將插入的記錄的ID返回到C#
Insert into dbo.file_row (file_sub_type) values (@file_sub_type)
DECLARE @result int;
SET @result = SCOPE_IDENTITY()
RETURN @result;
這工作正常,返回SSMS中的ID。但是,當我從C#調用它時,它返回-1。
var connection = GetSqlConnection();
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "InsertInto_file_row";
command.Parameters.Add(new SqlParameter("@file_sub_type", fileType));
int result = command.ExecuteNonQuery();
connection.Close();
return result;
我沒有看到我做錯了什麼。我只需要插入記錄的Id。
格雷格
使用'ExecuteScalar' - 'ExecuteNonQuery'將返回受影響的行。 –