SqlCeConnection.Close 方法
關閉資料來源的連接。這是關閉任何開啟連接的慣用方法。
命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)
語法
'宣告
Public Overrides Sub Close
'用途
Dim instance As SqlCeConnection
instance.Close()
public override void Close()
public:
virtual void Close() override
abstract Close : unit -> unit
override Close : unit -> unit
public override function Close()
實作
備註
Close 方法會回復任何暫止交易,然後再釋放連接。如果在處理 StateChange 事件時呼叫 Close,則不會引發任何其他的 StateChange 事件。
應用程式可以呼叫 Close 一次以上。不會有例外狀況 (Exception) 產生。
範例
下列範例會建立 SqlCeConnection、加以開啟、執行查詢,然後關閉連接。
Dim conn As SqlCeConnection = Nothing
Try
conn = New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")
conn.Open()
Dim cmd As SqlCeCommand = conn.CreateCommand()
cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')"
cmd.ExecuteNonQuery()
Finally
conn.Close()
End Try
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}