Telerik Reporting Error: sqlDataSource does not exist in the current context

Telerik Reporting Error: sqlDataSource does not exist in the current context

我正在尝试使用 Telerik Reporting 创建 SqlDataSource,但在尝试连接到 AdventureWorks 数据库时使用文档中的确切代码时遇到错误:

    SqlDataSource sqlDataSource = new SqlDataSource();
    sqlDataSource.ProviderName = "System.Data.SqlClient";
    sqlDataSource.ConnectionString = "Data Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";
    sqlDataSource.SelectCommand = "SELECT * FROM Production.Product";

代码放在 class 的正文中,但应该放在方法中。像这样使用对象初始值设定项:

SqlDataSource sqlDataSource = new SqlDataSource
{
    ProviderName = "System.Data.SqlClient",
    ConnectionString = "Data Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True",
    SelectCommand = "SELECT * FROM Production.Product"
}

或者将初始化代码移到单独的方法中。