为什么Datareader无法初始化? [英] Why Datareader cannot be initialized?

查看:81
本文介绍了为什么Datareader无法初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在类名之前使用abstract关键字来限制创建类的实例。

we use abstract keyword before classname to restric creating the instance of a class.

但是datareader不是抽象类,但是我们不能创建该类的实例。你能解释为什么吗?

But datareader is not an abstract class,but we cant create instance of that..can you explain why?

我搜索了它,然后发现它没有构造函数,这就是为什么我们不能创建对象的原因,但据我所知,如果没有构造函数,则编译器自动创建默认的构造函数。

I searched about it then I found it does not have constructor that's why we cannot create object but as per my knowledge if there is no constructor then compiler automatic create a default constructor.

请帮助...

推荐答案

DbDataReader 是一个抽象类。如果您的意思是 SqlDataReader 却没有公共构造函数,这就是为什么您无法创建实例的原因。它只有一个 内部 构造函数(ILSpy):

DbDataReader is an abstract class. If you mean SqlDataReader instead, it has no public constructor, that's why you can't create an instance. It has only an internal constructor (ILSpy):

// System.Data.SqlClient.SqlDataReader
internal SqlDataReader(SqlCommand command, CommandBehavior behavior)
{
    // ...
}

来自 MSDN


要创建 SqlDataReader ,您必须调用SqlCommand对象
ExecuteReader 方法,而不是直接使用构造函数。

To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, instead of directly using a constructor.

通常,最好避免实例化 DataReader ,因为仅需要通过 SqlCommand.ExecuteReader 创建。

In general it is a good idea to avoid instantiating a DataReader since it needs to be created via SqlCommand.ExecuteReader only.

这篇关于为什么Datareader无法初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆