Skip to main content
All docs
V24.2

Specify Data Connection

  • 2 minutes to read

Use Application Configuration File

The Data Source Wizard creates a connection string in the application configuration file and assigns a connection name to the SqlDataSource.ConnectionName property:

<configuration>  
...  
    <connectionStrings>  
        <add name="NWINDConnectionString"  
            connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" />  
    </connectionStrings>  
...  
</configuration>  
sqlDataSource1.ConnectionName = "NWINDConnectionString";  

Create a Connection String

You can create a custom string with connection parameters. The string should contain the XpoProvider parameter to identify the database provider type, and use syntax described in the following help topic section: Connection Strings for XPO Providers.

Create the CustomStringConnectionParameters instance and assign the string to the ConnectionString property:

string connectionString = @"XpoProvider=MSSqlServer;Data Source=(local);User ID=username;Password=password;Initial Catalog=database;Persist Security Info=true;";  
CustomStringConnectionParameters connectionParameters = new CustomStringConnectionParameters(connectionString);  
SqlDataSource ds = new SqlDataSource(connectionParameters);

Use a DataConnectionParametersBase Class Descendant

Create an instance of the DataConnectionParametersBase class descendant specific to the data provider (MsSqlConnectionParameters for Microsoft SQL Server, SQLiteConnectionParameters for SQLite database, and so on), and use the SqlDataSource.ConnectionParameters property to specify a data connection:

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
// ...
MsSqlConnectionParameters connectionParameters = new MsSqlConnectionParameters(){
    ServerName = "localhost",
    DatabaseName = "Northwind",
    UserName = "username",
    Password = "password",
    AuthorizationType = MsSqlAuthorizationType.SqlServer
};
SqlDataSource ds = new SqlDataSource();
ds.ConnectionName = "nwindConnection";
ds.ConnectionParameters = connectionParameters;

Do not leave the SqlDataSource.ConnectionName property empty; always assign a connection name. Otherwise, an exception occurs with the following message: Additional information: ‘null’ is not a valid value for ‘name’.