OleDbConnection.ConnectionTimeout 属性

获取在尝试建立连接时终止尝试并生成错误之前所等待的时间。

**命名空间:**System.Data.OleDb
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Overrides ReadOnly Property ConnectionTimeout As Integer
用法
Dim instance As OleDbConnection
Dim value As Integer

value = instance.ConnectionTimeout
public override int ConnectionTimeout { get; }
public:
virtual property int ConnectionTimeout {
    int get () override;
}
/** @property */
public int get_ConnectionTimeout ()
public override function get ConnectionTimeout () : int

属性值

等待连接打开的时间(以秒为单位)。默认值为 15 秒。

异常

异常类型 条件

ArgumentException

所设置的值小于 0。

备注

值 0 指示无限制,在 ConnectionString 中应避免值 0,否则会无限期地等待连接。

示例

下面的示例创建一个 OleDbConnection,并在连接字符串中设置它的一些属性。

' The connectionString argument contains the Connect Timeout 
' keywords, as follows: "... ;Connect Timeout=30;"
Public Sub InsertRow(ByVal connectionString As String, _
    ByVal insertSQL As String)

    Using connection As New OleDbConnection(connectionString)
        ' The insertSQL string contains a SQL statement that
        ' inserts a new row in the source table.
        Dim command As New OleDbCommand(insertSQL)

        ' Set the Connection to the new OleDbConnection.
        command.Connection = connection

        ' Open the connection and execute the insert command.
        Try
            connection.Open()
            command.ExecuteNonQuery()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub
// The connectionString argument contains the Connect Timeout 
// keywords, as follows: "... ;Connect Timeout=30;"
public void InsertRow(string connectionString, string insertSQL)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command.
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
    }

    // The connectionString argument contains the Connect Timeout 
    // keywords, as follows: "... ;Connect Timeout=30;"
    public void InsertRow(string connectionString, string insertSQL)
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            // The insertSQL string contains a SQL statement that
            // inserts a new row in the source table.
            OleDbCommand command = new OleDbCommand(insertSQL);

            // Set the Connection to the new OleDbConnection.
            command.Connection = connection;

            // Open the connection and execute the insert command.
            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            // The connection is automatically closed when the
            // code exits the using block.
        }

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

OleDbConnection 类
OleDbConnection 成员
System.Data.OleDb 命名空间
Database
DataSource

其他资源

连接到数据源