mdavi
mdavi

Reputation: 63

c# oracle: get RowId aftert Insert

Dears all,

I'm trying to return the Oracle RowId after an insert but the result is always null (even if the Insert is correct). Why? What's wrong?

Here below you can find the code:

cmd.CommandType = CommandType.Text;
cmd.AddParameter("ROWINSERTED", string.Empty, DBTypes.NVarchar2, ParameterDirection.InputOutput);
cmd.CommandText = $@"INSERT INTO EQUIPVAR(ID_VAR, ID_QU, XTGYREVAVALUE) VALUES(1, 1, 'TEST') RETURNING rowid INTO :ROWINSERTED";                
var res = cmd.ExecuteScalar();

I'm waiting the result inside the parameter ROWINSERTED

Thanks in advance.

Upvotes: 0

Views: 302

Answers (1)

fstam
fstam

Reputation: 699

Here's how you can do it with Dapper (nuget package):

DynamicParameters parameters = new DynamicParameters();
parameters.Add("out_id", dbType: DbType.Decimal, direction: ParameterDirection.Output);
database.Execute($@"INSERT INTO EQUIPVAR(ID_VAR, ID_QU, XTGYREVAVALUE) VALUES(1, 1, 'TEST') RETURNING rowid INTO :out_id", parameters);
int id = (int)parameters.Get<decimal>("out_id");

Upvotes: 1

Related Questions