-2

I'm using C# with .NET SqlDataReader(), SqlConnection(), SqlCommand() and .ExecuteReader() classes.

In vbScript, I can open RecordSets like this:

rs.open cSQL, cn, adOpenForwardonly, adLockReadonly

The rs.open statement can be given arguments to make the query ForwardOnly, and ReadOnly.

How do I do the same thing with C# and its SqlCommand() class?

KWallace
  • 624
  • 7
  • 15
  • You cannot really do this with `SqlCommand` - the question whether the current user can edit something or not depends on the **permissions** in the SQL Server database - not the access technology (ADO.NET, Entity Framework etc.) – marc_s Oct 20 '19 at 20:38

1 Answers1

1

In ADO.NET you handle it differently from the old days with cursors which you had to specify as forward only etc. The method you are looking for is called ExecuteReader. Consider the following code...

SqlCommand command = new SqlCommand(sqlQueryString, connection);
var reader = command.ExecuteReader();

This then gets you a SqlDataReader that contains your values and is essentially a forward only, read only structure.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
  • Oops. I am using the .ExecuteReader() method. Forgot to include that in the original post. I edited and added it. Thanks. – KWallace Oct 20 '19 at 20:54