I want to call a stored procedure from my database, but it also needs a List of attributes as a type and I placed a cursor on that type. But everytime I execute the command I get an exception back that the cursor is not open.
What I have now already
using (Connection = new SqlConnection(ConnectionString))
{
Connection.Open();
using (Command = new SqlCommand())
{
Command.Connection = Connection;
Command.CommandText = "CreateCharacter";
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.AddWithValue("@Skills", skills);
Command.Parameters.AddWithValue("@AccountId", accountId);
Command.Parameters.AddWithValue("@ClassId", masteryId);
Command.Parameters.AddWithValue("@RaceId", raceId);
Command.Parameters.AddWithValue("@Name", name);
try
{
return character = LoadCharacter(Convert.ToInt32(Command.ExecuteScalar()));
}
catch (Exception)
{
return null;
}
}
}
Type
CREATE TYPE dbo.AttributeList AS TABLE
(
AttributeId integer,
[Level] integer
);
GO
Stored Procedure
CREATE PROCEDURE dbo.CreateCharacter (@Attributes AS dbo.AttributeList
READONLY, @AccountId integer, @ClassId integer, @RaceId integer, @Name
varchar(12))
AS
BEGIN
DECLARE AttributeCursor CURSOR FOR
SELECT
AttributeId,
[Level]
FROM @Attributes
INSERT INTO Character
VALUES (@ClassId, @RaceId, NULL, @Name, 1, 0, 0)
DECLARE @Id integer
DECLARE @AttributeId integer
DECLARE @AttributeLevel integer
SET @Id = IDENT_CURRENT('Character');
UPDATE Account
SET CharacterId = @Id
WHERE Id = @AccountId
WHILE (@@FETCH_STATUS = 0)
BEGIN
INSERT INTO Attribute_Character
VALUES (@AttributeId, @Id, @AttributeLevel)
FETCH NEXT FROM AttributeCursor INTO @AttributeId, @AttributeLevel
END
RETURN @Id;
END