SqlConnectionStringBuilder.DataSource Property

Definition

Gets or sets the name or network address of the instance of SQL Server to connect to.

public:
 property System::String ^ DataSource { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter(typeof(Microsoft.Data.SqlClient.SqlConnectionStringBuilder+SqlDataSourceConverter))]
public string DataSource { get; set; }
public string DataSource { get; set; }
[<System.ComponentModel.TypeConverter(typeof(Microsoft.Data.SqlClient.SqlConnectionStringBuilder+SqlDataSourceConverter))>]
member this.DataSource : string with get, set
member this.DataSource : string with get, set
Public Property DataSource As String

Property Value

The value of the DataSource property, or String.Empty if none has been supplied.

Attributes

Exceptions

To set the value to null, use Value.

Examples

The following example demonstrates that the SqlConnectionStringBuilder class converts synonyms for the "Data Source" connection string key into the well-known key:

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(
            "Network Address=(local);Integrated Security=SSPI;" +
            "Initial Catalog=AdventureWorks");

        // Display the connection string, which should now 
        // contain the "Data Source" key, as opposed to the 
        // supplied "Network Address".
        Console.WriteLine(builder.ConnectionString);

        // Retrieve the DataSource property.
        Console.WriteLine("DataSource = " + builder.DataSource);

        Console.WriteLine("Press any key to continue.");
        Console.ReadLine();
    }
}

Remarks

This property corresponds to the "Data Source", "server", "address", "addr", and "network address" keys within the connection string. Regardless of which of these values has been supplied within the supplied connection string, the connection string created by the SqlConnectionStringBuilder will use the well-known "Data Source" key.

The port number can be specified after the server name: server=tcp:servername, portnumber.

When specifying a local instance, always use (local). To force a protocol, add one of the following prefixes:np:(local), tcp:(local), lpc:(local).

You can also connect to a LocalDB database as follows: server=(localdb)\\myInstance. For more information about LocalDB, see SqlClient Support for LocalDB. Data Source must use the TCP format or the Named Pipes format. TCP format is as follows:

  • tcp:<host name>\<instance name>
  • tcp:<host name>,<TCP/IP port number>

The TCP format must start with the prefix "tcp:" and is followed by the database instance, as specified by a host name and an instance name. This format is not applicable when connecting to Azure SQL Database. TCP is automatically selected for connections to Azure SQL Database when no protocol is specified.

The host name MUST be specified in one of the following ways:

  • NetBIOSName
  • IPv4Address
  • IPv6Address

The instance name is used to resolve to a particular TCP/IP port number on which a database instance is hosted. Alternatively, specifying a TCP/IP port number directly is also allowed. If both instance name and port number are not present, the default database instance is used.

The Named Pipes format is as follows:

  • np:\\<host name>\pipe\<pipe name>

The Named Pipes format MUST start with the prefix "np:" and is followed by a named pipe name.

The host name MUST be specified in one of the following ways:

  • NetBIOSName
  • IPv4Address
  • IPv6Address

The pipe name is used to identify the database instance to which the .NET application will connect.

If the value of the Network key is specified, the prefixes "tcp:" and "np:" should not be specified. Note: You can force the use of TCP instead of shared memory, either by prefixing tcp: to the server name in the connection string, or by using localhost.

Applies to