Well ... it's right and wrong.
It'll probably work, but ... it's also probably flawed as well.
A DataReader return rows, yes - but what it is designed for is returning multiple rows - and since you are only interested in a single value, then directly using
ExecuteScalar
instead is a better idea.
And since you don't name your column in the
SELECT
statement, it';s unlikely that the DataReader indexing will work.
But the biggy is that what you seem to be doing is trying to "pre-generate" an ID number before you insert a row, and that's dangerous: SQL is a multiuser system so what works fine in development with just you testing then fails and throws up horrible intermittent data corruption problems which are a bugger to fix because two different users pre-generated the same ID and used it later.
Instead, change your DB to use an
IDENTITY
field as the ID, and only access that in your C# code once you INSERT the new row using the
@@IDENTITY[
^] variable. SQL will ensure you get unique values only.