Export (0) Print
Expand All

SqlDataSource::ConnectionString Property

Gets or sets the ADO.NET provider–specific connection string that the SqlDataSource control uses to connect to an underlying database.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public:
virtual property String^ ConnectionString {
	String^ get ();
	void set (String^ value);
}
<asp:SqlDataSource ConnectionString="String" />

Property Value

Type: System::String
A .NET Framework data provider–specific string that the SqlDataSource uses to connect to the SQL database that it represents. The default is an empty string ("").

The SqlDataSource control can be used with a variety of ADO.NET providers and the syntax of the connection string that is used to connect to an underlying data source is specific to the provider.

When you configure a SqlDataSource control, you set the ProviderName property to the type of database (the default is System.Data.SqlClient), and you set the ConnectionString property to a connection string that includes information that is required in order to connect to the database. The contents of a connection string differ depending on what type of database the data source control is accessing. For example, the SqlDataSource control requires a server name, database (catalog) name, and information about how to authenticate the user when connecting to a SQL Server. For information about the contents of connection strings, see the ConnectionString property for the SqlConnection class, ConnectionString property for the OracleConnection class, ConnectionString property for the OleDbConnection class, or the ConnectionString property for the OdbcConnection class.

If you change the ConnectionString property, the DataSourceChanged event is raised, causing any controls that are bound to the SqlDataSource control to rebind.

Security noteSecurity Note

For information about storing a connection string, see How To: Secure Connection Strings when Using Data Source Controls.

This section contains two code examples. The first code example demonstrates how to set the ConnectionString property to connect to a Microsoft SQL Server database and display the results of the SelectCommand property in a GridView control. The second code example demonstrates a more complex scenario, where a SqlDataSource control is used to display and update data in a password-protected Microsoft Access database. In each case, the connectionStrings element of the Web.config file is shown first, followed by the ASP.NET page that contains the SqlDataSource control.

The following code example demonstrates how to set the ConnectionString property to connect to a SQL Server database and display the results of the SelectCommand property in a GridView control.

<connectionStrings>
  <add name="MyNorthwind" connectionString="server=foehammer;database=Northwind;user=north;password=north;" providerName="System.Data.SqlClient" />
</connectionStrings>
No code example is currently available or this language may not be supported.

The following code example demonstrates a more complex scenario than the preceding code example, where a SqlDataSource control is used to display and update data in a password-protected Access database. Because the SqlDataSource is used with Access, the ProviderName property is set to the System.Data.OleDb provider, and the ConnectionString property is set to an appropriate connection string for a UNC-shared Access database. A GridView control displays orders with shipment dates. You can update an order by checking the appropriate check box, and then clicking the Update button.

Important noteImportant

This example includes a password in plain text only for illustration purposes. In a production application, connection strings that include passwords should be encrypted. For more information, see Protecting Connection Information.

No code example is currently available or this language may not be supported.
<connectionStrings>
  <add name="MyPasswordProtectedAccess"
       connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\uncpath\Northwind_PasswordProtected.mdb;Mode=3;Jet OLEDB:Database Password=myPassword;"
       providerName="System.Data.OleDb" />
</connectionStrings>
No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft