简体   繁体   中英

Increment a SqlDataReader field value in C#

I am trying to increment a value from a SqlDataReader (SQL Server) to be passed later to a parametrized INSERT command (new record).

Is there an easier way on doing this code? It seem so long. Field indexx is bigint (negative value).

First Code:

string tmp_index = Sql_DR["indexx"].ToString().Trim();
long temp2_indexx = (Int64.Parse(tmp_index) + 1);

Second Code:

long temp2_indexx = Int64.Parse(Sql_DR["indexx"].ToString().Trim())+1;

What I wanted was something like...

long tmp_index = Sql_DR["indexx"] +1;

But I get an error:

Operator '+' cannot be applied to operands of type 'object' and 'int'

I know I can do auto increment on SQL Server itself but this is not the requirement because I'll be replacing the "+ 1" later with a different value (variable).

I've searched already, but the topics I found is not what I wanted.

long tmp_index = (long)Sql_DR["indexx"] +1;

Try the above. You have to cast it to a long since it's by default an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM