报表的装载和查看
一.使用CrystalReportViewer跟水晶报表Crystal Report的绑定浏览;
1.Solution Explorer/Add/Add New Item…/DataSet,按照提示建立DataSet;
2.Solution Explorer/Add/Add New Item…/Crystal Report,建立表文件;
3。然后再建立一个窗体文件 CrystalReport.aspx
   并在文件中建立或从工具箱中拖入一个CrystalReportViewer
4。在CrystalReport.aspx.cs中写入下列代码:
// 设置连接信息
string myConnectionString = "data source=RAINFISH;initial catalog=car;persist security info=False;user id=sa;workstation id=RAINFISH;packet size=4096";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string mySelectQuery = "select * from TbAutoDispatchBill";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery, myConnection);
//DataSet1为用XML 架构定义语言 (XSD) 描述的DataSet架构
DataSet1 dsBill = new DataSet1();
// 连接到数据库、从数据库中获取数据然后断开数据库连接
myDataAdapter.Fill(dsBill,"TbAutoDispatchBill");
//实例化水晶报表
CrystalReport_Bill CR_Bill = new CrystalReport_Bill();
//将填充的数据集传递给报表
CR_Bill.SetDataSource(dsBill);
//设置报表源,将该报表对象绑定到 Web 窗体查看器
CrystalReportViewer1.ReportSource=CR_Bill;----------------------------
如果数据库密码不为空的话,则应该添加以下代码安全登陆:
ReportDocument Report = new ReportDocument();
Report.Load(Server.MapPath("cr1.rpt"));
//设置按名称指定的表的 TableLogOnInfo 对象。 
TableLogOnInfo logOnInfo = new TableLogOnInfo();//对报表中的每个表进行循环
//报表中数据集的Table 对象的个数
for(int i=0;i==Report.DataBase.Tables.Count-1;i++)
{
     // 为报表中的表设置连接信息。
     
     // 设置服务器或 ODBC 数据源名称、数据库名称、
     // logonInfo.ConnectionInfo.ServerName = server
     // logonInfo.ConnectionInfo.DatabaseName = database   //设置用于登录到数据源的用户名
   logOnInfo.ConnectionInfo.UserId = "sa";
   //设置用于登录到数据源的“密码”
   logOnInfo.ConnectionInfo.Password = "yourpwd";
   // 将连接信息应用于表。
   Report.DataBase.Tables[i].ApplyLogOnInfo(logOnInfo);
}
CrystalReportViewer1.ReportSource = Report;
//当然如果密码为空的话也同样可以使用这种方法的-------------
具体的你自己去做调整了,以上是我自己整理出来的一些资料,都是测试通过的GOOD LUCK TO YOU!

解决方案 »

  1.   

    谢谢,老兄!
    Web报表我做成功了.但是我想要的是Windows窗体的报表!烦请指教!
      

  2.   

    忘了提示你一点:myDataAdapter.Fill(dsBill,"TbAutoDispatchBill");TbAutoDispatchBill为你的数据库表的实际表名如果还有什么问题,自己先好好想想,不行的话,再发上来吧,OK?
      

  3.   

    晕,windows的跟WEB的是相类似的啊,比WEB报表的还要容易啊,既然你已经将WEB报表做成功了,那么现在你在WINDOWS报表中碰上的具体问题是什么?就只是要把CrystalReport1的内容显示出来吗?
      

  4.   

    好的,我再想想!
    这是Web应用程序的吧,但是在Windows应用程序中,有好大的不同吧?
      

  5.   

    对啊!
    原来我按书上做的,每次运行时,就弹出来一个对话框,让我输入服务器名/用户名/密码,但是总是通不过!
    后来我就想用Web方式的连接方式,但是看了看,有好大的不同!
      

  6.   

    如果你已经有一个设计好的报表,你在WINDOWS APLICATION 将该报表作为强类型报表进行加载,然后绑定将之显示出来,看是否有问题,建议先将你的密码设为空来做测试,这个过程应该很快的我等会可能要休息了,晚上再来看你这帖了,:)
      

  7.   

    我按照你说的做了,但是编译时提示我"找不到类型或命名空间名称DataSet1(是否缺少using指令或程序集引用)",我百思不得其解!我的using指令包括了System;System.Drawing;System.Collections;ComponentModel;System.Windows.Forms;System.Data;System.Data.SqlClient;不知道还缺少什么?DataSet1是我创建了的啊!
      

  8.   

    这是全部的代码,由于我不知道应该在哪个地方绑定,于是我用了个蠢方法:放了一个按钮,然后把绑定的代码写进去!我想应该有另外的方法,等待答复!
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
    namespace WinReport
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    //public CrystalReport1 oRpt = new CrystalReport1();
    private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // crystalReportViewer1
    // 
    this.crystalReportViewer1.ActiveViewIndex = -1;
    this.crystalReportViewer1.Location = new System.Drawing.Point(8, 56);
    this.crystalReportViewer1.Name = "crystalReportViewer1";
    this.crystalReportViewer1.ReportSource = null;
    this.crystalReportViewer1.Size = new System.Drawing.Size(712, 288);
    this.crystalReportViewer1.TabIndex = 0;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(208, 16);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(728, 349);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.crystalReportViewer1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    // 设置连接信息
    string myConnectionString = "data source=CC;initial catalog=GlobalMarket;persist security info=False;user id=sa;workstation id=CC;packet size=4096";
    SqlConnection myConnection = new SqlConnection(myConnectionString);
    string mySelectQuery = "select * from Orders";
    SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery, myConnection);
    //DataSet1为用XML 架构定义语言 (XSD) 描述的DataSet架构
    Dataset1 dsBill = new Dataset1();
    // 连接到数据库、从数据库中获取数据然后断开数据库连接
    myDataAdapter.Fill(dsBill,"Orders");
    //实例化水晶报表
    CrystalReport1 oRpt = new CrystalReport1();
    //将填充的数据集传递给报表
    oRpt.SetDataSource(dsBill);
    //设置报表源,将该报表对象绑定到 Web 窗体查看器
    crystalReportViewer1.ReportSource = oRpt; }
           }
    }
      

  9.   

    你将你的代码放在Form_Load()事件中就可以载入了,呵呵,没问题了吧