如何使用SqlCommand()在C#中指定ReadOnly查询

问题描述 投票:-2回答:1

我正在将C#与.NET SqlDataReader()SqlConnection()SqlCommand().ExecuteReader()类一起使用。

在vbScript中,我可以这样打开RecordSet:

rs.open cSQL, cn, adOpenForwardonly, adLockReadonly

rs.open语句可以被赋予参数以使查询成为ForwardOnly和ReadOnly。

我如何使用C#及其SqlCommand()类执行相同的操作?

c# .net sql-server sqlcommand
1个回答
1
投票

在ADO.NET中,您与以前使用游标(只能将其指定为仅向前等)进行处理的方式不同。要查找的方法称为ExecuteReader。考虑下面的代码...

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

然后,您将获得一个SqlDataReader,其中包含您的值,并且实质上是一个仅向前的只读结构。

© www.soinside.com 2019 - 2024. All rights reserved.