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()
實作
IDataReader. . :: . .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();
}