原由:客户希望将财务软件中的应收应付款项打印出来(原财务软件对此项打印的支持不是很好)思路:通过直接连财务软件的数据库(ACCESS),用SQL提取数据并在MicrosoftReportViewer中显示出来,最后实现报表的打印。难点:本人第一次接触程序,说写的程序是从网咯上拷贝下来的,对C#也不熟悉。。愿景:我将贴出程序的所有源代码,希望CSDN上的朋友们能找出程序中的问题所在。。
   
      目前程序编译通过,但是提示:“ExecuteReader要求已打开且可用的连接。连接的当前状态为关闭”
   
      程序中肯定还有很多的错误,希望朋友们能找出。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;using System.Data.SqlClient;
using Microsoft.Reporting.WinForms;namespace 鸿基大酒店应收应付报表系统
{
    public partial class Form1 : Form
    {
        private System.ComponentModel.Container components = null;
 // private System.Data.DataSet myDataSet; 
        
        public Form1()
        {
            InitializeComponent();                    }        private void Form1_Load(object sender, EventArgs e)
        {            this.reportViewer1.RefreshReport();
        }        private void button1_Click(object sender, EventArgs e)
        {
            string kjqj = "";
                switch(comboBox2.Text )
                {
                    case "一月":
                        kjqj = "1";
                        break;
                    case "二月":
                        kjqj = "2";
                        break;
                    case "三月":
                        kjqj = "3";
                        break;
                    case "四月":
                        kjqj = "4";
                        break;
                    case "五月":
                        kjqj = "5";
                        break;
                    case "六月":
                        kjqj = "6";
                        break;
                    case "七月":
                        kjqj = "7";
                        break;
                    case "八月":
                        kjqj = "8";
                        break;
                    case "九月":
                        kjqj = "9";
                        break;
                    case "十月":
                        kjqj = "10";
                        break;
                    case "十一月":
                        kjqj = "11";
                        break;
                    case "十二月":
                        kjqj = "12";
                        break; 
                }
                    
                                    
                   
                 //创建一个 OleDbConnection对象        
           string cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\\鸿基大酒店.ais;Persist Security Info=True;Password=ypbwkfyjhyhgzj;User ID=morningstar;Jet OLEDB:System database=j:\\System.mda;Jet OLEDB:Database Password=ypbwkfyjhyhgzj "  ;    
                  
                 //sql查询
                 string strCom = "SELECT GLObj.FObjID as 序号 ,  GLObj.FObjName as  客户名称 ,  GLBal.FEndBal as 应收账款  FROM GLObj INNER JOIN (GLAcct INNER JOIN GLBal ON GLAcct.FAcctID = GLBal.FAcctID) ON (GLObj.FObjID = GLBal.FObjID) AND (GLObj.FClsID = GLBal.FClsID) WHERE GLObj.FObjID<>'*' AND  GLBal.FObjID<>'*' AND GLBal.FCyID<>'*' AND GLBal.FAcctID='113' and glbal.fperiod="; 
                 strCom =strCom+kjqj ;                            try
  {
    
        OleDbConnection conReport = new OleDbConnection(cnString );
    OleDbCommand cmdReport = new OleDbCommand(strCom, conReport);  //sql语句          //    SqlDataReader drReport;
    OleDbDataReader drReport = cmdReport.ExecuteReader();
    DataSet dsReport = new 应收账款();    //打开连接
    conReport.Open();    //准备连接对象以把获取的数据放入数据集    cmdReport.CommandType = CommandType.Text;
    cmdReport.Connection = conReport;
    cmdReport.CommandText = strCom ;    //从命令对象中读取数据
    drReport = cmdReport.ExecuteReader();    //有了ADO.NET,可把读取来的数据直接加载到数据集中    dsReport.Tables[0].Load(drReport);    //关闭读取及连接
    drReport.Close();
    conReport.Close();    //为查看器提供本地报表数据
    reportViewer1.LocalReport.ReportEmbeddedResource =
    "宏基大酒店应收账款明表.rdlc";    //准备报表数据源
    ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource();
    rds.Name = "鸿基应收账款明细表";
    rds.Value = dsReport.Tables[0];
    reportViewer1.LocalReport.DataSources.Add(rds);    //加载报表查看器
    reportViewer1.RefreshReport();
  }
  catch (Exception ex)
  {
    //显示错误信息
    MessageBox.Show(ex.Message);
  }
  finally
  {
    //检查连接是否仍然打开,如果是,关闭它。
   //if (conReport.State == ConnectionState.Open)
    //{
       // conReport.Close();
   // }
  }
                
    }
}
}

解决方案 »

  1.   


    //    SqlDataReader drReport;
        OleDbDataReader drReport = cmdReport.ExecuteReader();
        DataSet dsReport = new 应收账款();    //打开连接
        conReport.Open();
    换成
        //打开连接
        conReport.Open();
    //    SqlDataReader drReport;
        OleDbDataReader drReport = cmdReport.ExecuteReader();    DataSet dsReport = new 应收账款();
      

  2.   

    问题解决了,我将DataReader换成了DAtaset!
    源代码如下public Form1()
            {
                InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)
            {
                //this.rpt_yszk.RefreshReport();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string kjqj = "";
                switch (comboBox2.Text)
                {
                    case "一月":
                        kjqj = "1";
                        break;
                    case "二月":
                        kjqj = "2";
                        break;
                    case "三月":
                        kjqj = "3";
                        break;
                    case "四月":
                        kjqj = "4";
                        break;
                    case "五月":
                        kjqj = "5";
                        break;
                    case "六月":
                        kjqj = "6";
                        break;
                    case "七月":
                        kjqj = "7";
                        break;
                    case "八月":
                        kjqj = "8";
                        break;
                    case "九月":
                        kjqj = "9";
                        break;
                    case "十月":
                        kjqj = "10";
                        break;
                    case "十一月":
                        kjqj = "11";
                        break;
                    case "十二月":
                        kjqj = "12";
                        break;
                }            //sql查询
                string strCom = "SELECT GLObj.FObjID as 序号 ,  GLObj.FObjName as  客户名称 ,  GLBal.FEndBal as 应收账款 ,glbal.fperiod as 会计期间 FROM GLObj INNER JOIN (GLAcct INNER JOIN GLBal ON GLAcct.FAcctID = GLBal.FAcctID) ON (GLObj.FObjID = GLBal.FObjID) AND (GLObj.FClsID = GLBal.FClsID) WHERE GLObj.FObjID<>'*' AND  GLBal.FObjID<>'*' AND GLBal.FCyID<>'*' AND GLBal.FAcctID='113' and glbal.fperiod=";
                strCom = strCom + kjqj;            try
                {                //使用水晶报表
                    //
                    //1.绝对路径连接
                    OleDbConnection dbconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\\鸿基大酒店.ais;Persist Security Info=True;Password=ypbwkfyjhyhgzj;User ID=morningstar;Jet OLEDB:System database=j:\\System.mda;Jet OLEDB:Database Password=ypbwkfyjhyhgzj ");
                    dbconn.Open();
                    //操作数据,用dataset
                    OleDbDataAdapter da = new OleDbDataAdapter(strCom, dbconn);                应收账款  ds = new 应收账款();//这里的Dataset是前面自己定义的数据集
                    da.Fill(ds, "yszk");//一定要写明是放到哪个table中,否则数据根本不会读到自己定义的数据集中,why?                //da.Fill(ds);//如果这样写,根本就得不到数据,虽然不知道是为什么会这样,可是,对dataset1,却确实是这样的
                   // textBox1.Text = ds.Tables[0].Rows[0][0].ToString();                rpt_yszk  orpt= null;//对应自己定义的报表.rpt的名字
                    orpt = new rpt_yszk();                orpt.SetDataSource(ds);//在代码页面中,把报表跟数据集关联起来,在报表中还有一处设置的地方                crystalReportViewer1.ReportSource = orpt ;//相当于设置一个报表控件上面显示的是哪个报表
                    //关闭数据库的连接
                    dbconn.Close();
                }
                finally
                {
                }        }