亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

奇怪的SqlDataReader

奇怪的SqlDataReader

C#
躍然一笑 2018-12-16 17:09:35
問題來源于一個非常初級的錯誤,在寫一個數據庫訪問代碼時 不小心寫成了SqlDataReader reader=new SqlDataReader()結果編譯器報錯:“未定義構造函數”,誰都知道SqlDataReader是不能這樣被實例化的,改成SqlDataReader reader=cmd.ExecuteDataReader()就完,但是心中的疑問確不由產生,第一是個編譯器報的錯“未定義構造函數”,開始以為我沒有寫實參,但是intellsence告訴我根本就沒有什么參數!誒這就奇怪了,沒有參數的構造函數不是系統就默認有的嘛,那為什么不能這樣new?還有就是既然不能new 那在ExecuteDataReader()中這個SqlDataReader的對象是如何生成的???請高人指點迷津!
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

MSDN幫助原文: 若要創建 SqlDataReader,必須調用 SqlCommand 對象的 ExecuteReader 方法,而不要直接使用構造函數。 ExecuteDataReader() 的函數體: public SqlDataReader ExecuteReader() { SqlStatistics statistics = null; SqlDataReader reader; IntPtr ptr; Bid.ScopeEnter(out ptr, "<sc.SqlCommand.ExecuteReader|API> %d#", this.ObjectID); try { statistics = SqlStatistics.StartTimer(this.Statistics); reader = this.ExecuteReader(CommandBehavior.Default, "ExecuteReader"); } finally { SqlStatistics.StopTimer(statistics); Bid.ScopeLeave(ref ptr); } return reader; } SqlDataReader的構造函數: internal SqlDataReader(SqlCommand command, CommandBehavior behavior) { this._recordsAffected = -1; this.ObjectID = Interlocked.Increment(ref _objectTypeCount); this._command = command; this._commandBehavior = behavior; if (this._command != null) { this._timeoutSeconds = command.CommandTimeout; this._connection = command.Connection; if (this._connection != null) { this._statistics = this._connection.Statistics; this._typeSystem = this._connection.TypeSystem; } } this._dataReady = false; this._metaDataConsumed = false; this._hasRows = false; this._browseModeInfoConsumed = false; } 不是沒有構造函數,不給你用罷了

查看完整回答
反對 回復 2018-12-20
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

private SqlDataReader(){} 這樣就不能使用構造函數了

查看完整回答
反對 回復 2018-12-20
  • 2 回答
  • 0 關注
  • 610 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號