摘要如下:
1。第一步是添加新项crystalreport1
2。在crystalreport1.rpt面页右键选择:”数据库”à”添加/删除数据库”出现下面对话框:
然后点开ole db(ado)的加号,将弹出选择数据库的对话框
其中”项目数据”里面的”当前连接”显示的就是ole db(ado)的连接,这样就可以将表添加到右边的”选定的表”中了(切记:不要既从”ole db” 中选表或命令又从”当前连接”中选,如果你用了数据集,也是一样的道理,否则全提示出”登陆失败”的错误!)
3。字段资料管理器里面”数据库字段”里把字段直接拖到crystalreport.rpt里面了.布置好布局.
4。回到webform1.aspx页面,选择控件crystalreportviewer将其放到页面上。
5。代码
using crystaldecisions.shared;
using crystaldecisions.crystalreports.engine;
在webform1.aspx.cs的form_load里面添加代码:
tablelogoninfo logoninfo = new tablelogoninfo();
reportdocument rpdmy0= new reportdocument();
logoninfo.connectioninfo.servername = “nnn”;
logoninfo.connectioninfo.password=”dong”;
logoninfo.connectioninfo.databasename=”lab”;
logoninfo.connectioninfo.userid = “sa”;
string path = server.mappath(“crystalreport2.rpt”);
rpdmy0.load(path);
rpdmy0.database.tables[0].applylogoninfo(logoninfo);
crystalreportviewer1.reportsource = rpdmy0;
其中:
tablelogoninfo类是: 提供属性,以便检索和设置表连接选项.它有一个连接的属性connectioninfo,用来设置与数据库的连接.如上面的servername等.生成一个报表文档对象将前面我们做好的那个crystalreport1.rpt赋给它.在这里要注意的是database属性!然后把报表对象做为crystalreportviewer1的数据源.
或者,获得数据库的方法改为直接从数据库获得dataset
using system.data.sqlclient;
using system.collections.specialized;
string strconn=system.configuration.configurationsettings.appsettings[“db”].tostring();
using (sqlconnection conn = new sqlconnection(strconn))
{
sqldataadapter da = new sqldataadapter(strsql,conn);//strsql是sql语言”select * from dbo.table1″
dataset ds=new dataset();
da.fill( ds);
crystalreport1 cr1 = new crystalreport1();
cr1.setdatasource( ds);
crystalreportviewer1.reportsource = cr1;
}
using crystaldecisions.shared;
using crystaldecisions.crystalreports.engine;
在webform1.aspx.cs的form_load里面添加代码:
tablelogoninfo logoninfo = new tablelogoninfo();
reportdocument rpdmy0= new reportdocument();
logoninfo.connectioninfo.servername = “nnn”;
logoninfo.connectioninfo.password=”dong”;
logoninfo.connectioninfo.databasename=”lab”;
logoninfo.connectioninfo.userid = “sa”;
string path = server.mappath(“crystalreport2.rpt”);
rpdmy0.load(path);
rpdmy0.database.tables[0].applylogoninfo(logoninfo);
crystalreportviewer1.reportsource = rpdmy0;
其中:
tablelogoninfo类是: 提供属性,以便检索和设置表连接选项.它有一个连接的属性connectioninfo,用来设置与数据库的连接.如上面的servername等.生成一个报表文档对象将前面我们做好的那个crystalreport1.rpt赋给它.在这里要注意的是database属性!然后把报表对象做为crystalreportviewer1的数据源.
或者,获得数据库的方法改为直接从数据库获得dataset
using system.data.sqlclient;
using system.collections.specialized;
string strconn=system.configuration.configurationsettings.appsettings[“db”].tostring();
using (sqlconnection conn = new sqlconnection(strconn))
{
sqldataadapter da = new sqldataadapter(strsql,conn);//strsql是sql语言”select * from dbo.table1″
dataset ds=new dataset();
da.fill( ds);
crystalreport1 cr1 = new crystalreport1();
cr1.setdatasource( ds);
crystalreportviewer1.reportsource = cr1;
}
using crystaldecisions.shared;
using crystaldecisions.crystalreports.engine;
在webform1.aspx.cs的form_load里面添加代码:
tablelogoninfo logoninfo = new tablelogoninfo();
reportdocument rpdmy0= new reportdocument();
logoninfo.connectioninfo.servername = “nnn”;
logoninfo.connectioninfo.password=”dong”;
logoninfo.connectioninfo.databasename=”lab”;
logoninfo.connectioninfo.userid = “sa”;
string path = server.mappath(“crystalreport2.rpt”);
rpdmy0.load(path);
rpdmy0.database.tables[0].applylogoninfo(logoninfo);
crystalreportviewer1.reportsource = rpdmy0;
其中:
tablelogoninfo类是: 提供属性,以便检索和设置表连接选项.它有一个连接的属性connectioninfo,用来设置与数据库的连接.如上面的servername等.生成一个报表文档对象将前面我们做好的那个crystalreport1.rpt赋给它.在这里要注意的是database属性!然后把报表对象做为crystalreportviewer1的数据源.
或者,获得数据库的方法改为直接从数据库获得dataset
using system.data.sqlclient;
using system.collections.specialized;
string strconn=system.configuration.configurationsettings.appsettings[“db”].tostring();
using (sqlconnection conn = new sqlconnection(strconn))
{
sqldataadapter da = new sqldataadapter(strsql,conn);//strsql是sql语言”select * from dbo.table1″
dataset ds=new dataset();
da.fill( ds);
crystalreport1 cr1 = new crystalreport1();
cr1.setdatasource( ds);
crystalreportviewer1.reportsource = cr1;
}