当前位置:编程学习 > C#/ASP.NET >>

vs2010 DataSet取数来源是存储过程时,无法取到临时表的列信息

如题:
存储过程如下:
Create Proc p as 
begin
   Create Table #t(id int identity(1,1),c1 varchar(10),c2 varchar(10))
   Insert into #t(c1,c2) values('aaa','111')
   Insert into #t(c1,c2) values('bbb','222')
   select c1,c2 from #t
   drop table #t
end
在vs里新建dataset, 命令类型选择“使用现有存储过程”,select下拉框选择‘P'后“向导结果”窗口里提示“已生成select语句,对象名’#t’无效”返回后在得到的table p 中没有列的信息,但将临时表的类型改为全局临时表可以。请高手指教 --------------------编程问答-------------------- 你可以这样:
   

 public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

查询数据的时候这样写:
  /// <summary>
        /// GetInfoByKey
        /// </summary>
        /// <param name="type"></param>
        /// <param name="sono"></param>
        /// <param name="soline"></param>
        /// <param name="pono"></param>
        /// <param name="poline"></param>
        /// <returns></returns>
        public DataTable GetInfoByKey(string type,string sono, string soline, string pono, string poline)
        {
            string strSql = "CMBC.DBO.usp_GetInfoByKey ";
            SqlParameter[] sp = new SqlParameter[] 
            {
               new SqlParameter("@TYPE",type),
               new SqlParameter("@SONO",sono),
               new SqlParameter("@SOLINE",soline),
               new SqlParameter("@PONO",pono),
               new SqlParameter("@POLINE",poline),
            };
            return base.GetDataSet(strSql, sp);
        }

这样的话不管是临时表还是什么表都可以的,我一直都这样用,通过代码可能好很多。

--------------------编程问答-------------------- 你可以这样:
   

 public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

查询数据的时候这样写:
  /// <summary>
        /// GetInfoByKey
        /// </summary>
        /// <param name="type"></param>
        /// <param name="sono"></param>
        /// <param name="soline"></param>
        /// <param name="pono"></param>
        /// <param name="poline"></param>
        /// <returns></returns>
        public DataTable GetInfoByKey(string type,string sono, string soline, string pono, string poline)
        {
            string strSql = "CMBC.DBO.usp_GetInfoByKey ";
            SqlParameter[] sp = new SqlParameter[] 
            {
               new SqlParameter("@TYPE",type),
               new SqlParameter("@SONO",sono),
               new SqlParameter("@SOLINE",soline),
               new SqlParameter("@PONO",pono),
               new SqlParameter("@POLINE",poline),
            };
            return base.GetDataSet(strSql, sp);
        }

这样的话不管是临时表还是什么表都可以的,我一直都这样用,通过代码可能好很多。

--------------------编程问答-------------------- 你可以这样:
   

 public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

查询数据的时候这样写:
  /// <summary>
        /// GetInfoByKey
        /// </summary>
        /// <param name="type"></param>
        /// <param name="sono"></param>
        /// <param name="soline"></param>
        /// <param name="pono"></param>
        /// <param name="poline"></param>
        /// <returns></returns>
        public DataTable GetInfoByKey(string type,string sono, string soline, string pono, string poline)
        {
            string strSql = "CMBC.DBO.usp_GetInfoByKey ";
            SqlParameter[] sp = new SqlParameter[] 
            {
               new SqlParameter("@TYPE",type),
               new SqlParameter("@SONO",sono),
               new SqlParameter("@SOLINE",soline),
               new SqlParameter("@PONO",pono),
               new SqlParameter("@POLINE",poline),
            };
            return base.GetDataSet(strSql, sp);
        }

这样的话不管是临时表还是什么表都可以的,返回一个DataTable,需要取哪一列的值就随你自己的了,我一直都这样用,通过代码可能好很多。

--------------------编程问答-------------------- 好长时间没用sql了,学习一下 --------------------编程问答-------------------- 你可以这样:
   

 public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

查询数据的时候这样写:
  /// <summary>
        /// GetInfoByKey
        /// </summary>
        /// <param name="type"></param>
        /// <param name="sono"></param>
        /// <param name="soline"></param>
        /// <param name="pono"></param>
        /// <param name="poline"></param>
        /// <returns></returns>
        public DataTable GetInfoByKey(string type,string sono, string soline, string pono, string poline)
        {
            string strSql = "CMBC.DBO.usp_GetInfoByKey ";
            SqlParameter[] sp = new SqlParameter[] 
            {
               new SqlParameter("@TYPE",type),
               new SqlParameter("@SONO",sono),
               new SqlParameter("@SOLINE",soline),
               new SqlParameter("@PONO",pono),
               new SqlParameter("@POLINE",poline),
            };
            return base.GetDataSet(strSql, sp);
        }

这样的话不管是临时表还是什么表都可以的,返回一个DataTable,需要取哪一列的值就随你自己的了,我一直都这样用,通过代码可能好很多。

--------------------编程问答-------------------- 你可以这样:
   

 public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

查询数据的时候这样写:
  /// <summary>
        /// GetInfoByKey
        /// </summary>
        /// <param name="type"></param>
        /// <param name="sono"></param>
        /// <param name="soline"></param>
        /// <param name="pono"></param>
        /// <param name="poline"></param>
        /// <returns></returns>
        public DataTable GetInfoByKey(string type,string sono, string soline, string pono, string poline)
        {
            string strSql = "CMBC.DBO.usp_GetInfoByKey ";
            SqlParameter[] sp = new SqlParameter[] 
            {
               new SqlParameter("@TYPE",type),
               new SqlParameter("@SONO",sono),
               new SqlParameter("@SOLINE",soline),
               new SqlParameter("@PONO",pono),
               new SqlParameter("@POLINE",poline),
            };
            return base.GetDataSet(strSql, sp);
        }

这样的话不管是临时表还是什么表都可以的,返回一个DataTable,需要取哪一列的值就随你自己的了,我一直都这样用,通过代码可能好很多。

--------------------编程问答--------------------
引用 1 楼 chenpeng0118 的回复:
我要建RepoertViewer他的数据源只能是dataset,有解决方法吗?



你可以这样:
   

 public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection)……
--------------------编程问答--------------------
我晕,第一次回复楼主的帖子的时候CSDN提示当前无法响应我的请求,所以我就多点击了几次,没想到重复回复了这么多次,真是不好意思! --------------------编程问答--------------------
引用 8 楼 chenpeng0118 的回复:
我晕,第一次回复楼主的帖子的时候CSDN提示当前无法响应我的请求,所以我就多点击了几次,没想到重复回复了这么多次,真是不好意思!

没关系,我要建DataSet作为设计时RepoertViewer的数据源,有解决方法吗? --------------------编程问答--------------------
引用 7 楼 wangzefei 的回复:
引用 1 楼 chenpeng0118 的回复:

我要建RepoertViewer他的数据源只能是dataset,有解决方法吗?



你可以这样:


public DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand……



DataSet和DataTable其实查不多的啊,
只不过DataSet可以包含多张表而已,你取值的时候可以这样:DataTable dt=ds.Table[表的索引];

RepoertViewer?你是想做水晶报表吗?我把我的报表代码给你看一下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.OracleClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using System.Data.Common;
using System.Collections.Generic;
using CrystalDecisions.CrystalReports.Engine;

public partial class Report : System.Web.UI.Page
{
    private static string sConn = "Data Source=DB;Password=123123;User ID=sa;";
    static string StartTime = string.Empty;
    static string EndTime = string.Empty;
    static string DepartmentName = string.Empty;
    static string SeriesName = string.Empty;
    static string ModuleName = string.Empty;
    static string ck_model = string.Empty;

    ReportDocument myReport;

    protected void Page_Load(object sender, EventArgs e)
    {
        StartTime = Request.QueryString["startTime"];
        EndTime = Request.QueryString["endTime"];
        DepartmentName = Request.QueryString["departmentName"];
       
        string Sql = string.Empty;

      
            Sql = "SELECT * FROM TABLE ";
      
               
               
        DataSetQAStatReport DS = new DataSetQAStatReport();
     
        OracleConnection dc = new OracleConnection(sConn);
        OracleDataAdapter da = new OracleDataAdapter();
        da = new OracleDataAdapter(Sql, dc);
        da.Fill(DS, "SampleInfo");


        //使用报表对象加载报表
        myReport = new ReportDocument();
        string reportPath = Server.MapPath("~/app_data/Report.rpt");
        myReport.Load(reportPath);


        //绑定数据集,注意,一个报表用一个数据集。
        myReport.SetDataSource(DS);
        CrystalReportViewer1.ReportSource = myReport;
        CrystalReportViewer1.DataBind();        
    }

    private void Page_Unload(object sender, EventArgs e)
    {
        myReport.Dispose();//rd为这个页面定义的CrystalDecisions.CrystalReports.Engine.ReportDocument rd;
    }
}
--------------------编程问答-------------------- 您这是在运行时指定数据源,我是在设计ReportViewer时使用,ReportViewer是微软在vs2010里替代水晶报表的一个报表控件

引用 10 楼 chenpeng0118 的回复:
引用 7 楼 wangzefei 的回复:
引用 1 楼 chenpeng0118 的回复:

我要建RepoertViewer他的数据源只能是dataset,有解决方法吗?



你可以这样:


public DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = ne……
--------------------编程问答--------------------
引用 10 楼 chenpeng0118 的回复:
引用 7 楼 wangzefei 的回复:
引用 1 楼 chenpeng0118 的回复:

我要建RepoertViewer他的数据源只能是dataset,有解决方法吗?



你可以这样:


public DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new……




VS2010我没有试过,但是一个报表对应一个DataSet啊,然后将Dataset里面的字段拉到页面上面就好了,就是设计ReportViewer时使用,这个是VS2008的做法,你试试看VS2010里面能不能这样做,
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,