SqlCeDataReader.Close メソッド
SqlCeDataReader オブジェクトを閉じます。
名前空間: System.Data.SqlServerCe
アセンブリ: System.Data.SqlServerCe (System.Data.SqlServerCe.dll)
構文
'宣言
Public Overrides Sub Close
'使用
Dim instance As SqlCeDataReader
instance.Close()
public override void Close()
public:
virtual void Close() override
abstract Close : unit -> unit
override Close : unit -> unit
public override function Close()
実装
説明
SqlCeConnection を他の操作で使用するには、SqlCeDataReader を使い終わったときに、Close メソッドを明示的に呼び出す必要があります。ただし、同じ接続で複数のリーダーを作成できます。
使用例
SqlCeConnection、SqlCeCommand、および SqlCeDataReader を作成する例を次に示します。この例では、データを読み取り、コンソールに出力します。最後に、この例では SqlCeDataReader を閉じてから、SqlCeConnection を閉じます。
Dim conn As SqlCeConnection = Nothing
Dim cmd As SqlCeCommand = Nothing
Dim rdr As SqlCeDataReader = Nothing
Try
' Open the connection and create a SQL command
'
conn = New SqlCeConnection("Data Source = AdventureWorks.sdf")
conn.Open()
cmd = New SqlCeCommand("SELECT * FROM DimEmployee", conn)
rdr = cmd.ExecuteReader()
' Iterate through the results
'
While rdr.Read()
Dim employeeID As Integer = rdr.GetInt32(0) ' or: rdr["EmployeeKey"];
Dim lastName As String = rdr.GetString(5) ' or: rdr["FirstName"];
End While
' Always dispose data readers and commands as soon as practicable
'
rdr.Close()
cmd.Dispose()
Finally
' Close the connection when no longer needed
'
conn.Close()
End Try
SqlCeConnection conn = null;
SqlCeCommand cmd = null;
SqlCeDataReader rdr = null;
try
{
// Open the connection and create a SQL command
//
conn = new SqlCeConnection("Data Source = AdventureWorks.sdf");
conn.Open();
cmd = new SqlCeCommand("SELECT * FROM DimEmployee", conn);
rdr = cmd.ExecuteReader();
// Iterate through the results
//
while (rdr.Read())
{
int employeeID = rdr.GetInt32(0); // or: rdr["EmployeeKey"];
string lastName = rdr.GetString(5); // or: rdr["FirstName"];
}
// Always dispose data readers and commands as soon as practicable
//
rdr.Close();
cmd.Dispose();
}
finally
{
// Close the connection when no longer needed
//
conn.Close();
}