日期:2014-05-20 浏览次数:20942 次
//定义水晶报表的数据库连接信息
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = @"C:\My Documents\Visual Studio 2005\WebSites\CrystalReportsWebSite2\employee.mdb"; //实际路径
//将数据库信息传递给报表
SetDBLogonForReport(connectionInfo, myReport);
private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
string ConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\Database\Database1.mdb"; //我这边的.mdb数据库放在启动项目bin->Debug->Database文件夹下
OleDbConnection conn = new OleDbConnection(ConnString);
string strSQL = "select * from table";//这边select的字段名及类型要和数据集中的数据表结构(名称及类型)保持一致
OleDbDataAdapter da = new OleDbDataAdapter(strSQL, conn);
DataSet1 ds = new DataSet1();
da.Fill(ds.TableA); //填充到数据集DataSet1的TableA表(我数据集新建了一个TableA表)
CrystalReport1 myReport = new CrystalReport1();
myReport.SetDataSource(ds); //为报表设置数据源
crystalReportViewer1.ReportSource = myReport;