今天做的报表涉及到一个报表里使用3个数据表,如何将这三个表的数据映射到fastreport中呢,看下面代码。对应的要在fastreport中将表改成这个名字,数据库连接信息就可以不用保存到报表中了。
btnExportBargain.Enabled = false;
int paperId = Convert.ToInt32(ucDataGridView1.Rows[ucDataGridView1.CurrentRow.Index].Cells[0].Value.ToString());
string conStr = BaseSystemInfo.ConnectionString;
try
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = con;
sqlcmd.CommandText = "select * from [VW_报表打印] where Id=" + paperId+"; select * from VW_报表打印_详细记录 where 询价单id=" + paperId + "; select * from VW_报表打印_详细记录 where 询价单id=" + paperId + " and 是否采用=1";;
;
SqlDataAdapter sda = new SqlDataAdapter(sqlcmd);
sda.TableMappings.Add("Table", "Table");
sda.TableMappings.Add("Table1", "T2");
sda.TableMappings.Add("Table2", "T3");
ds1 = new DataSet();
sda.Fill(ds1);
con.Close();
sda.Dispose();
}
catch (Exception err)
{
MessageBox.Show(err.StackTrace);
}
try
{
FastReport.Report report = new FastReport.Report();
string filename = @"D:\12.合同商定记录.frx";
report.Load(filename);
report.RegisterData(ds1);
report.GetDataSource("Table").Enabled = true;
report.GetDataSource("T2").Enabled = true;
report.GetDataSource("T3").Enabled = true;
report.Show();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
btnExportBargain.Enabled = true;