private void LoadViewsIntoComboBox()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "SELECT name FROM sys.views";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["name"].ToString());
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error while loading views: " + ex.Message);
}
finally
{
connection.Close();
}
}
}
private void LoadReport(string viewName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = $"SELECT * FROM {viewName}";
SqlDataAdapter da = new SqlDataAdapter(query, connection);
System.Data.DataTable dt = new System.Data.DataTable();
da.Fill(dt);
reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource reportDataSource = new ReportDataSource("aracTakipDataSet", dt);
reportViewer1.LocalReport.DataSources.Add(reportDataSource);
reportViewer1.LocalReport.ReportPath = @"\aracTakip.Report1.rdlc";
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
MessageBox.Show("Error while loading report: " + ex.Message);
}
finally
{
connection.Close();
}
}
}
private void raporlama_Load(object sender, EventArgs e)
{
this.dataTable1TableAdapter.Fill(this.dataSet1.DataTable1);
LoadViewsIntoComboBox();
this.reportViewer1.RefreshReport();
}
我尝试将视图添加到数据集并添加相关表,并且尝试在它和表适配器之间设置查询,但我无法破译它。
cbReportSelection.SelectedIndexChanged += cbReportSelection_SelectedIndexChanged;
private void cbReportSelection_SelectedIndexChanged(object sender, EventArgs e)
{ // Select or dowload data
}
现在要看情况了。如果数据库不是那么大并且没有太多变化,只需下载它并保持同步即可。这里一个不错的选择是将数据映射到类/对象并创建与 CB 的数据绑定,这样实际上“真实”数据就可以通过事件作为 Eventargument 来触发 - 无需先获取它 - 并到达所有订阅者。也节省了很多辅助功能。另一方面,如果数据库太大,只需创建您的类并预先制作所有查询,以便用更改的事件触发。有很多方法可以实现这种行为,太多了..甚至有更多的方法来组织数据/查询。