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