SqlConnection 构造函数

定义

初始化 SqlConnection 类的新实例。

重载

SqlConnection()

初始化 SqlConnection 类的新实例。

SqlConnection(String)

如果给定包含连接字符串的字符串,则初始化 SqlConnection 类的新实例。

SqlConnection(String, SqlCredential)

在给定连接字符串的情况下,初始化 SqlConnection 类的新实例,该连接字符串不使用 Integrated Security = true 和包含用户 ID 和密码的 SqlCredential 对象。

SqlConnection()

初始化 SqlConnection 类的新实例。

public:
 SqlConnection();
public SqlConnection ();
Public Sub New ()

示例

以下示例创建并打开 SqlConnection

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file, using the
    // System.Configuration.ConfigurationManager.ConnectionStrings property
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;";
}
Private Sub OpenSqlConnection()
    Dim connectionString As String = GetConnectionString()
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
        Console.WriteLine("State: {0}", connection.State)
    End Using
End Sub

Private Function GetConnectionString() As String
    ' To avoid storing the connection string in your code,  
    ' you can retrieve it from a configuration file, using the
    ' System.Configuration.ConfigurationManager.ConnectionStrings property
    Return "Data Source=(local);Database=AdventureWorks;" _
      & "Integrated Security=SSPI;"
End Function

注解

创建新的 实例 SqlConnection 时,读/写属性将设置为以下初始值,除非使用属性中的 ConnectionString 关联关键字专门设置它们。

属性 初始值
ConnectionString 空字符串 (“”)
ConnectionTimeout 15
Database 空字符串 (“”)
DataSource 空字符串 (“”)

只能使用 ConnectionString 属性更改这些属性的值。 类 SqlConnectionStringBuilder 提供用于创建和管理连接字符串内容的功能。

另请参阅

适用于

SqlConnection(String)

如果给定包含连接字符串的字符串,则初始化 SqlConnection 类的新实例。

public:
 SqlConnection(System::String ^ connectionString);
public SqlConnection (string connectionString);
new System.Data.SqlClient.SqlConnection : string -> System.Data.SqlClient.SqlConnection
Public Sub New (connectionString As String)

参数

connectionString
String

用于打开 SQL Server 数据库的连接。

例外

提供的连接字符串参数验证失败ConnectionString

示例

以下示例创建并打开 SqlConnection

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        Console.WriteLine("State: {0}", connection.State);
        Console.WriteLine("ConnectionString: {0}",
            connection.ConnectionString);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file, using the
    // System.Configuration.ConfigurationSettings.AppSettings property
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;";
}
Private Sub OpenSqlConnection()
    Dim connectionString As String = GetConnectionString()

    Using connection As New SqlConnection(connectionString)

        connection.Open()

        Console.WriteLine("State: {0}", connection.State)
        Console.WriteLine("ConnectionString: {0}", _
            connection.ConnectionString)
    End Using
End Sub

Private Function GetConnectionString() As String
    ' To avoid storing the connection string in your code,  
    ' you can retrieve it from a configuration file, using the
    ' System.Configuration.ConfigurationSettings.AppSettings property
    Return "Data Source=(local);Database=AdventureWorks;" _
      & "Integrated Security=SSPI;"
End Function

注解

创建新的 实例 SqlConnection 时,读/写属性将设置为以下初始值,除非使用属性中的 ConnectionString 关联关键字专门设置它们。

属性 初始值
ConnectionString connectionString
ConnectionTimeout 15
Database 空字符串 (“”)
DataSource 空字符串 (“”)

只能使用 ConnectionString 属性更改这些属性的值。 类 SqlConnection 提供用于创建和管理连接字符串内容的功能。

另请参阅

适用于

SqlConnection(String, SqlCredential)

在给定连接字符串的情况下,初始化 SqlConnection 类的新实例,该连接字符串不使用 Integrated Security = true 和包含用户 ID 和密码的 SqlCredential 对象。

public:
 SqlConnection(System::String ^ connectionString, System::Data::SqlClient::SqlCredential ^ credential);
public SqlConnection (string connectionString, System.Data.SqlClient.SqlCredential credential);
new System.Data.SqlClient.SqlConnection : string * System.Data.SqlClient.SqlCredential -> System.Data.SqlClient.SqlConnection
Public Sub New (connectionString As String, credential As SqlCredential)

参数

connectionString
String

不使用 ContextConnection = true 或以下任何连接字符串关键字的连接字符串:Integrated Security = trueUserIdPassword

credential
SqlCredential

SqlCredential 对象。 如果 credential 为 null,则 SqlConnection(String, SqlCredential) 在功能上等效于 SqlConnection(String)

例外

提供的连接字符串参数验证失败ConnectionString

另请参阅

适用于