I use Enterprise Library and I have one problem:
string sql = "
UPDATE StackOverflow SET UserName = @UserName
WHERE Id = @Id
";
DbCommand cmd = base.Database.GetSqlStringCommand(sql);
base.Database.AddInParameter(cmd, "Id", DbType.Int32, StackOverflow.Id);
base.Database.AddInParameter(cmd, "UserName", DbType.Int32, StackOverflow.UserName);
int val = Convert.ToInt32(base.Database.ExecuteScalar(cmd));
Convert.ToInt32(base.Database.ExecuteScalar(cmd)) //returns 0.
I’ve read this article http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx
The article says:
The function returns the new Identity column value if a new row was inserted, 0 on failure.
but I did not insert into that table – I only want to update and return updated row Id.
You should use ExecuteNonQuery in your case.
ExecuteScalar
ExecuteNonQuery
Your query doesn’t return anything, so
ExecuteScalaris not the right method to work with.ExecuteNonQuery on the other side will give the correct information if your query has updated anything.