SqlDataReader sdr = new SqlDataReader();
SqlCommand SCmd = new SqlCommand();
sdr =SCmd.ExecuteReader(CommandBehavior.CloseConnection);
其中對於CommandBehavior的說明如下:
提供對查詢結果和查詢對資料庫的影響的說明。
CommandBehavior是一個枚舉。其成員和說明分別如下:
1、public static const System.Data.CommandBehavior CloseConnection
System.Data.CommandBehavior 的成員
摘要:
在執行該命令時,如果關閉關聯的 DataReader 對象,則關聯的 Connection 對象也將關閉。
2、public static const System.Data.CommandBehavior Default
System.Data.CommandBehavior 的成員
摘要:
此查詢可能返回多個結果集。執行查詢可能會影響資料庫狀態。Default 不設定 System.Data.CommandBehavior 標誌,因此調用 ExecuteReader(CommandBehavior.Default) 在功能上等效於調用 ExecuteReader()。
3、public static const System.Data.CommandBehavior KeyInfo
System.Data.CommandBehavior 的成員
摘要:
此查詢返回列和主鍵資訊。執行此查詢時不鎖定選定的行。 當使用 KeyInfo 時,用於 SQL Server 的 .NET Framework 資料提供者將 FOR BROWSE 子句追加到正在執行的語句。使用者應該注意潛在的副作用,例如對 SET FMTONLY ON 語句的使用產生的幹擾。有關更多資訊,請參見“SQL Server 聯機圖書”。
4、public static const System.Data.CommandBehavior SchemaOnly
System.Data.CommandBehavior 的成員
摘要:
此查詢只返回列資訊,而不影響資料庫狀態。
5、public static const System.Data.CommandBehavior SequentialAccess
System.Data.CommandBehavior 的成員
摘要:
提供一種方法,以便 DataReader 處理包含帶有大二進位值的列的行。SequentialAccess 不是載入整行,而是使 DataReader 將資料作為流來載入。然後可以使用 GetBytes 或 GetChars 方法來指定開始讀取操作的位元組位置以及正在返回的資料的有限的緩衝區大小。 當指定 SequentialAccess 時,儘管無需讀取每個列,但是需要按照列的返回順序讀取它們。一旦已經讀過返回的資料流中某個位置的內容,就不能再從 DataReader 中讀取該位置或該位置之前的資料。當使用 System.Data.OleDb.OleDbDataReader 時,可重新讀取當前列的值,直到讀過它。當使用 System.Data.SqlClient.SqlDataReader 時,一次只能讀取一個列值。
6、public static const System.Data.CommandBehavior SingleResult
System.Data.CommandBehavior 的成員
摘要:
查詢返回一個結果集。
7、public static const System.Data.CommandBehavior SingleRow
System.Data.CommandBehavior 的成員
摘要:
查詢應返回一行。執行查詢可能會影響資料庫狀態。一些 .NET Framework 資料提供者可能(但不要求)使用此資訊來最佳化命令的效能。在用 System.Data.OleDb.OleDbCommand 對象的 System.Data.OleDb.OleDbCommand.ExecuteReader 方法指定 System.Data.CommandBehavior.SingleRow 時,用於 OLE DB 的 .NET Framework 資料提供者使用 OLE DB IRow 介面(如果可用)執行綁定。否則,它使用 IRowset 介面。如果您的 SQL 陳述式應該只返回一行,則指定 System.Data.CommandBehavior.SingleRow 還可以提高應用程式效能。 在執行返回多個結果集的查詢時,可以指定 SingleRow。在這種情況下,仍返回多個結果集,但每個結果集只有一行。
而其枚舉具體定義如下:
public enum CommandBehavior |
| Name: |
System.Data.CommandBehavior |
| Assembly: |
System.Data, Version=1.0.5000.0 |
[Flags]public enum CommandBehavior{ // Fields CloseConnection = 0x20, Default = 0, KeyInfo = 4, SchemaOnly = 2, SequentialAccess = 0x10, SingleResult = 1, SingleRow = 8} |