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。不会生成任何异常。
示例
下面的示例创建一个 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();
}