从存储过程收集多个结果集时出错

时间:2015-02-23 12:28:10

标签: c# sql-server stored-procedures

我有一个我写过的存储过程。我正在尝试从中收集多个结果集。但不幸的是我收到了错误

  

System.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的第一次机会异常。

消息是我一直看到的错误。

我已尽力而为但不知道错误是什么,我已经测试了我的存储过程它运行正常。

存储过程:

 CREATE PROCEDURE [dbo].[Collectcompanyservicewithpackages]
     @CompanyID int     
 AS
     select * 
     from Serviceduration 
     where Client_CompanyID = @CompanyID;

     select Serviceitem.* 
     from Serviceduration, Serviceitem
     where Client_CompanyID = @CompanyID 
       and Serviceduration.ServicedurationID = Serviceitem.ServicedurationID;

     select ServiceitemPackage.* 
     from Serviceduration, Serviceitem, ServiceitemPackage
     where Client_CompanyID = @CompanyID 
       and Serviceduration.ServicedurationID = Serviceitem.ServicedurationID 
       and Serviceitem.ServiceitemID = ServiceitemPackage.ServiceitemID

数据库模块:

片段

public List<Serviceview> GetFirmServiceswithpackages_sp(int CompanyID)
{
        List<Serviceview> allservices = null;

        IEnumerable<Serviceduration> servicedurations = null;

        IEnumerable<Serviceitem> serviceitems = null;

        IEnumerable<ServiceitemPackage> serviceitempackages = null;

        using(context){

            Debug.WriteLine("App got here for starters .........................");


            // If using Code First we need to make sure the model is built before we open the connection 
            // This isn't required for models created with the EF Designer 
            //context.Database.Initialize(force: false); 

            // Create a SQL command to execute the sproc
            var cmd = context.Database.Connection.CreateCommand();
            cmd.CommandText = "Collectcompanyservicewithpackages  @CompanyID";
            cmd.CommandType = CommandType.StoredProcedure;

            DbParameter inputparameter = new SqlParameter();
           inputparameter.DbType = DbType.Int64;
           inputparameter.ParameterName = "CompanyID";
           inputparameter.Value = CompanyID;
           inputparameter.Direction = ParameterDirection.Input;

           cmd.Parameters.Add(inputparameter);

            try
            {
                // Run the sproc
                context.Database.Connection.Open();
                var reader = cmd.ExecuteReader();

                // Read Blogs from the first result set
                var durations = ((IObjectContextAdapter)context)
                    .ObjectContext
                    .Translate<Serviceduration>(reader, "Servicedurations", MergeOption.AppendOnly);  

                servicedurations = this.Extractdurations(durations);

                Debug.WriteLine("No of duration "+servicedurations.Count());

                // Move to second result set and read Serviceitems in cart 
                reader.NextResult();

                var services4sale = ((IObjectContextAdapter)context)
                    .ObjectContext
                    .Translate<Serviceitem>(reader, "Serviceitems", MergeOption.AppendOnly);

                serviceitems = this.Extractservices(services4sale);

                Debug.WriteLine("No of services  "+serviceitems.Count() );

                // Move to second result set and read Serviceitems in cart 
                reader.NextResult();

                var packages = ((IObjectContextAdapter)context)
                    .ObjectContext
                    .Translate<ServiceitemPackage>(reader, "ServiceitemPackages", MergeOption.AppendOnly);

                serviceitempackages = this.Extractpackages(packages);

                Debug.WriteLine("No of packages ...."+ serviceitempackages.Count()); 

                allservices = this.ReturnServiceincart(servicedurations, serviceitems, serviceitempackages);

            }
            catch(SqlException e){

                Debug.WriteLine("Cause of the error "+e.InnerException.Message);
            }
            finally
            {
                context.Database.Connection.Close();
            }
        }

        return allservices;
    }

//以下方法用于从Object Resultset中提取Enumerables。我很乐意称他们为辅助方法

   //This is the help method that would help us to do the final crafting of our stored procedure run around
    private List<Serviceview> ReturnServiceincart(IEnumerable<Serviceduration> durations, IEnumerable<Serviceitem> services, IEnumerable<ServiceitemPackage> packages)
    {
        List<Serviceview> allservices = new List<Serviceview>();

        if(services != null){

            foreach(var service in services){

                Serviceview view = new Serviceview()
                {
                    Name = service.Name,
                    Cost = service.Cost,
                    Description = service.Description,
                    Durationname = durations.Where(item=>item.ServicedurationID == service.ServicedurationID).Select(item=>item.Duration).SingleOrDefault<string>(),
                    IsVisible = service.IsVisible,
                    Packages = Returnpackages(service.ServiceitemID, packages)  //Note we are not passing service.ServiceitemPackage, cos we are avoiding a lazy loading cos it already been returned in stored procedure.
                };
            }
        }

        return allservices;
    }

    private IEnumerable<Serviceduration> Extractdurations(ObjectResult<Serviceduration> durations)
    {
        IEnumerable<Serviceduration> servicedurations = durations.AsEnumerable<Serviceduration>();

        return servicedurations;
    }

    private IEnumerable<Serviceitem> Extractservices(ObjectResult<Serviceitem> services4sale)
    {
        IEnumerable<Serviceitem> serviceitems = services4sale.AsEnumerable<Serviceitem>();

        return serviceitems;
    }

    private IEnumerable<ServiceitemPackage> Extractpackages(ObjectResult<ServiceitemPackage> packages)
    {
        IEnumerable<ServiceitemPackage> servicepackages = packages.AsEnumerable<ServiceitemPackage>();

        return servicepackages;
    }

    //Because the packages here would contain all packages for all company services we need to filter with a service ID
    private List<string> Returnpackages(int ServiceitemID, IEnumerable<ServiceitemPackage> packageitems)
    {
        List<string> packages = new List<string>();

        foreach(var package in packageitems){
            if(package.ServiceitemID == ServiceitemID)
               packages.Add(package.PackageName);
        }

        return packages;
    }

以下所做的更改    cmd.CommandText =“Collectcompanyservicewithpackages”;

对查询进行更改后。我收到错误消息

查询结果不能多​​次枚举。

新错误在行

allservices = this.ReturnServiceincart(servicedurations,serviceitems,serviceitempackages);

1 个答案:

答案 0 :(得分:0)

cmd.CommandText =“Collectcompanyservicewithpackages”;

这条线解决了魔法。

在此之后我关闭了命令和读者对象。

现在工作正常。