views:

69

answers:

2

In my project I use System.Data.SQLite. Database has table Tags, which contains autoincrement primary field ID (type Integer). When I write:

using (SQLiteCommand command = conn.CreateCommand())
{
   command.CommandText = "insert into Tags(name) values(@name) returning into @id";
   command.Parameters.Add("@id", DbType.Int32).Direction = ParameterDirection.Output;
   command.ExecuteNonQuery();
}

Visual Studio said that the operation is not supported. How to fix it?
UPDATE
Error occurs on line:

command.Parameters.Add("@id", DbType.Int32).Direction = ParameterDirection.Output;

UPDATE #2
Working code:

SELECT last_insert_rowid()
+1  A: 

I advice to use Stored Procedure.

IN SQL Server has @@IDENTITY system Variable . it returns the last autoincrement value

CREATE PROCEDURE SPGetLastAutoInc @name varchar, @lastAutoResult INT OUTPUT AS

INSERT INTO Tags(name) values(@name)


SET @lastAutoResult = @@IDENTITY

-- Return the number of all items ordered.
RETURN lastAutoResult 
GO
AEMLoviji
SQLite has not @@IDENTITY
Dublicator
Sorry. i did not know that you are using SQLite . It works in SqlServer
AEMLoviji
+1  A: 

I found working query:

SELECT last_insert_rowid()
Dublicator