共用方式為


SqlCeConnection.Open 方法

開啟資料庫連接,且其屬性設定是由 ConnectionString 指定。

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
<SecurityCriticalAttribute> _
<SecurityTreatAsSafeAttribute> _
Public Overrides Sub Open
'用途
Dim instance As SqlCeConnection

instance.Open()
[SecurityCriticalAttribute]
[SecurityTreatAsSafeAttribute]
public override void Open()
[SecurityCriticalAttribute]
[SecurityTreatAsSafeAttribute]
public:
virtual void Open() override
[<SecurityCriticalAttribute>]
[<SecurityTreatAsSafeAttribute>]
abstract Open : unit -> unit 
[<SecurityCriticalAttribute>]
[<SecurityTreatAsSafeAttribute>]
override Open : unit -> unit 
public override function Open()

實作

IDbConnection.Open()

例外狀況

例外狀況 條件
InvalidOperationException

連接已經開啟。

SqlCeException

當開啟連接時發生的連接層級錯誤。

備註

這個方法會建立資料來源的新連接。

注意

如果 SqlCeConnection 超出範圍,就不會關閉。您必須呼叫 CloseDispose 來明確地關閉連接。

範例

下列範例會建立 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();
}

請參閱

參考

SqlCeConnection 類別

System.Data.SqlServerCe 命名空間

Close