动态创建数据集以填充 Visual Studio 上的报告

Create dinamically a Dataset to fill reports on Visual Studio

我正在寻找一种方法来创建要绑定到我的报告的动态数据集
(我在 Visual Studio 2015 社区上使用 C#)。 谁能解释我该怎么做? 第一个想法是从查询创建数据集,然后将其绑定到我的报告, 但是我不能在不告诉 VS 数据集的情况下创建报告(它必须使用静态 ConnectionString 连接到数据库(VS 对我来说只是向导,我不知道如何动态地做到这一点)

我想要的示例代码:

DataSet myReportDS = ADO.getDS("SELECT * FROM" + 
"Table1 JOIN Table2 ON Table1.pkey = Table2.fkey");  
    //here I am stuck because I don't know even how to add objects without a 
    //static connection to my report (Designer) and how to bind it.

还要记住,DBMS 是 PostgreSQL。

非常感谢。

您可以使用 SqlDataAdapterOleDbDataAdapter

例如:

// Assumes that connection is a valid SqlConnection object.
string queryString = 
  "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");

msdn 站点上有更多示例。

来源: https://msdn.microsoft.com/en-us/library/bh8kx08z(v=vs.110).aspx

最后找到这篇文章解决了我的问题
https://blogs.msdn.microsoft.com/magreer/2008/10/16/setting-the-datasource-for-a-report-at-runtime/ 事实上,问题在于 VS 强制用户拥有静态数据集以允许创建报告。所以基本上我必须在本地环境中创建我的数据库模式的副本,然后我正常执行我的查询然后使用 Report.Fill() 方法。只要模式相同,它就可以工作!