none
报表的问题,请大家指点! RRS feed

  • 问题

  • using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using CrystalDecisions.Shared;
    using CrystalDecisions.CrystalReports.Engine;
    using DB_Operation;
    using System.Data.SqlClient;
    using System.Web.Configuration;
    using System.Data.OleDb;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //string conString = "Data Source=SERVER;Initial Catalog=EngineerInfo;Persist Security Info=True;User ID=web;Password=df;";
            //string sql = "select EmployeeId1 as EmployeeId,hisDogName1 as hisDog from forSubReport1";
            //SqlConnection conn = new SqlConnection(conString);
            //SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            //conn.Open();
            //DataSet1 ds1 = new DataSet1();
            //sda.Fill(ds1, "forSubReport");

            //sql = "SELECT EmployeeId1 as EmployeeId,EmployeeName1 as EmployeeName From Employee1";
            //sda.Fill(ds1, "Employee");

            //sql = "SELECT ProductId1 as ProductId,ProductName1 as ProductName From Product1";
            //sda.Fill(ds1, "Product");

            //sql = "SELECT EmployeeId1 as EmployeeId,ProductId1 as ProductId,saledate1 as saledate,num1 as num From sales1";
            //sda.Fill(ds1, "Sales");
    以上是我尝试的一种方式,不行


            //DataSet1 ds1 = new DataSet1();
            //OleDbDataAdapter da = new OleDbDataAdapter();
            //String connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SERVER;Initial Catalog=EngineerInfo;Persist Security Info=True;User ID=web;Password=df;";
            //OleDbConnection cn = new OleDbConnection(connstr);
            //da = new OleDbDataAdapter("SELECT EmployeeId1 as EmployeeId,Hisdogname1 as Hisdog From forsubreport1", cn);

            //da.Fill(ds1, "forsubreport");


            //da = new OleDbDataAdapter("SELECT EmployeeId1 as EmployeeId,EmployeeName1 as EmployeeName From Employee1", cn);
            //da.Fill(ds1, "Employee");
            //da = new OleDbDataAdapter("SELECT ProductId1 as ProductId,ProductName1 as ProductName From Product1", cn);
            //da.Fill(ds1, "Product");

            //da = new OleDbDataAdapter("SELECT EmployeeId1 as EmployeeId,ProductId1 as ProductId,saledate1 as saledate,num1 as num From sales1", cn);
            //da.Fill(ds1, "Sales");

    以上是我尝试的另外一种方式,也不行!

     

            DataSet1 ds1 = new DataSet1();
            string cmd = "SELECT EmployeeId1 as EmployeeId,Hisdogname1 as Hisdog From forsubreport1";
            DBConnection db = new DBConnection();
            ds1.Tables.Add(db.getDataSet(cmd, "forsubreport").Tables[0].Copy());
            // ds1.Merge(db.getDataSet(cmd, "forsubreport"));
            cmd = "";
            cmd = "SELECT EmployeeId1 as EmployeeId,EmployeeName1 as EmployeeName From Employee1";
            //ds1.Merge(db.getDataSet(cmd, "Employee"));
            ds1.Tables.Add(db.getDataSet(cmd, "Employee").Tables[0].Copy());

            cmd = "SELECT ProductId1 as ProductId,ProductName1 as ProductName From Product1";
            // ds1.Merge(db.getDataSet(cmd, "Product"));
            ds1.Tables.Add(db.getDataSet(cmd, "Product").Tables[0].Copy());

            cmd = "SELECT EmployeeId1 as EmployeeId,ProductId1 as ProductId,saledate1 as saledate,num1 as num From sales1";
            // ds1.Merge(db.getDataSet(cmd, "Sales"));
            ds1.Tables.Add(db.getDataSet(cmd, "Sales").Tables[0].Copy());

    以上是我根据大家的提示,还是不行!但已经不报错了,提示:您请求的报表需要更多信息.下面就是数据库登录,这样意味着我的数据集为空?

    以下是用的access数据库,也就是一个例子,可以运行!原来的数据集我没动,只是把数据库改为sql2003,然后就是上面一系列的尝试,始终通不过,    
        DataSet1 ds1 = new DataSet1();
            OleDbDataAdapter da = new OleDbDataAdapter();
            String connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath ("~/app_data/test.mdb")+";";
            OleDbConnection cn = new OleDbConnection(connstr);
            da = new OleDbDataAdapter("SELECT EmployeeId1 as EmployeeId,Hisdogname1 as Hisdog From forsubreport1", cn);

            da.Fill(ds1, "forsubreport");

           
            da = new OleDbDataAdapter("SELECT EmployeeId1 as EmployeeId,EmployeeName1 as EmployeeName From Employee1", cn);
            da.Fill(ds1, "Employee");

            da = new OleDbDataAdapter("SELECT ProductId1 as ProductId,ProductName1 as ProductName From Product1", cn);
            da.Fill(ds1, "Product");

            da = new OleDbDataAdapter("SELECT EmployeeId1 as EmployeeId,ProductId1 as ProductId,saledate1 as saledate,num1 as num From sales1", cn);
            da.Fill(ds1, "Sales");




            ReportDocument myReport = new ReportDocument();
            string reportPath = Server.MapPath("~/app_data/crystalreport1.rpt");
            myReport.Load(reportPath);
            myReport.SetDataSource(ds1);
            CrystalReportViewer1.ReportSource = myReport;

     

     


        }
    }

    2009年5月21日 7:27

答案

  • 你的方法不行,是因为你每次都实例化了一个新的 DataSet,你可以把 DataSet 做为一个方法参数代入到方法中,应该就可以了,至少下面的代码
    using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText1, fConnectionString)) { fAdapter.Fill(fDataSet,"forsubreport"); }
    1 using 声明一个代码块区域,当这段化码快执行完毕那么会自动释放 SqlDataAdapter,而且上术代码不需要你去关心连接打开与关闭,会自动处理的
    2 每次的 Fill 都向 DataSet 中添加了一个表, 你把你的方法改为如下代码也能达到一样的效果

            public void getDataSet(DataSet fDataSet, string sql, string tableName)
            {
                conn = new SqlConnection(conString);
                sda = new SqlDataAdapter(sql, conn);
                conn.Open();
                try
                {
                    sda.Fill(fDataSet, tableName);
                }
                catch
                {
                    conn.Close();
                }
                finally
                {
                    conn.Close();
                }
            }

    知识改变命运,奋斗成就人生!
    2009年5月25日 3:31
    版主

全部回复

  • 数据集里的四个表forsubreport是个独立的,有两列employeeid,hisdog;其他三个表定义了关系,仅外键约束
    2009年5月21日 7:36
  • 没人愿意指点一下我吗?孟老大可否有空?

    2009年5月22日 0:47
  • 有没人告诉我到底应该怎么连接??谢谢!
    2009年5月25日 2:06
  • 楼主,你好

    关于ADO.NET链接数据库方式情参考下面网页。
    http://www.cnblogs.com/wennxxin/archive/2008/11/16/1334695.html
    Microsoft Online Community Support
    2009年5月25日 2:41
  • 你好,你这样试一下

    DataSet fDataSet = new DataSet();
    string fConnectionString = "Data Source=SERVER;Initial Catalog=EngineerInfo;Persist Security Info=True;User ID=web;Password=df;";
    string fCommandText1 = "SELECT EmployeeId1 as EmployeeId,Hisdogname1 as Hisdog From forsubreport1";
    string fCommandText2 = "SELECT EmployeeId1 as EmployeeId,EmployeeName1 as EmployeeName From Employee1";
    string fCommandText3 = "SELECT ProductId1 as ProductId,ProductName1 as ProductName From Product1";
    string fCommandText4 = "SELECT EmployeeId1 as EmployeeId,ProductId1 as ProductId,saledate1 as saledate,num1 as num From sales1";
    
    using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText1, fConnectionString)) { fAdapter.Fill(fDataSet, "forsubreport"); }
    using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText2, fConnectionString)) { fAdapter.Fill(fDataSet, "Employee"); }
    using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText3, fConnectionString)) { fAdapter.Fill(fDataSet, "Product"); }
    using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText4, fConnectionString)) { fAdapter.Fill(fDataSet, "Sales"); }
    
    ReportDocument myReport = new ReportDocument();
    string reportPath = Server.MapPath("~/app_data/crystalreport1.rpt");
    myReport.Load(reportPath);
    myReport.SetDataSource(fDataSet);
    CrystalReportViewer1.ReportSource = myReport;


    知识改变命运,奋斗成就人生!
    2009年5月25日 2:57
    版主
  • 楼上哥们,你的方法可以,谢谢!

    private static string conString = "Data Source=SERVER;Initial Catalog=EngineerInfo;Persist Security Info=True;User ID=web;Password=df";
           
            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataAdapter sda = null;

            public DataSet getDataSet(string sql,string tableName)
            {
                conn = new SqlConnection(conString);
                sda = new SqlDataAdapter(sql, conn);
                conn.Open();
                DataSet ds = new DataSet();
                try
                {
                    sda.Fill(ds,tableName);
                }
                catch
                {
                    conn.Close();
                }
                finally
                {
                    conn.Close();
                }
                return ds;
            }
    但我用类这样写,为什么不行呢??

    2009年5月25日 3:17
  • 还有,using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText1, fConnectionString)) { fAdapter.Fill(fDataSet, "forsubreport"); }
    是什么用法?能给简单的说一下吗?谢谢!
    2009年5月25日 3:18
  • 我这样为什么不行呢???
     //string conString = "Data Source=SERVER;Initial Catalog=EngineerInfo;Persist Security Info=True;User ID=web;Password=df;";
            //string sql = "select EmployeeId1 as EmployeeId,hisDogName1 as hisDog from forSubReport1";
            //SqlConnection conn = new SqlConnection(conString);
            //SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            //conn.Open();
            //DataSet1 ds1 = new DataSet1();
    2009年5月25日 3:25
  • 楼主,你好

    静态成员只能为静态方法所用。另外using()这种方式用于那些实现了IDispose接口的,这样.net会自动对其进行释放回收,不需要你调用close方法。
    Microsoft Online Community Support
    2009年5月25日 3:31
  • 你的方法不行,是因为你每次都实例化了一个新的 DataSet,你可以把 DataSet 做为一个方法参数代入到方法中,应该就可以了,至少下面的代码
    using (SqlDataAdapter fAdapter = new SqlDataAdapter(fCommandText1, fConnectionString)) { fAdapter.Fill(fDataSet,"forsubreport"); }
    1 using 声明一个代码块区域,当这段化码快执行完毕那么会自动释放 SqlDataAdapter,而且上术代码不需要你去关心连接打开与关闭,会自动处理的
    2 每次的 Fill 都向 DataSet 中添加了一个表, 你把你的方法改为如下代码也能达到一样的效果

            public void getDataSet(DataSet fDataSet, string sql, string tableName)
            {
                conn = new SqlConnection(conString);
                sda = new SqlDataAdapter(sql, conn);
                conn.Open();
                try
                {
                    sda.Fill(fDataSet, tableName);
                }
                catch
                {
                    conn.Close();
                }
                finally
                {
                    conn.Close();
                }
            }

    知识改变命运,奋斗成就人生!
    2009年5月25日 3:31
    版主