//连接字符串
string connstr = "Data Source=.;Initial Catalog=WWALMDB;User ID=sa;Password=sqlsql";
//新建连接
SqlConnection connection = new SqlConnection(connstr);
//查询命令
SqlCommand command = new SqlCommand(textBoxSQL.Text, connection);
//适配器
SqlDataAdapter adapter = new SqlDataAdapter(command);
//数据集
DataSet ds = new DataSet();
//查询并填充
try
{
connection.Open();
adapter.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
command.Dispose();
connection.Dispose();
}
//指定rdlc报表
this.reportViewer1.LocalReport.ReportEmbeddedResource = "FormReport.EventReport.rdlc";
//清空reportviewer先前的数据源
this.reportViewer1.LocalReport.DataSources.Clear();
//新建数据源
ReportDataSource rs = new ReportDataSource();
rs.Name = "EventHistory";//"EventHistory"是rdlc报表Tablib属性下的数据集名称
rs.Value = ds.Tables[0];
//添加数据源
this.reportViewer1.LocalReport.DataSources.Add(rs);
//显示数据
this.reportViewer1.RefreshReport();