How to use ormlite with SQL Server and Mars?

c# dapper mars ormlite-servicestack


ServiceStack aficionados, hello!

We are legion (I hope so), so please help a brother out :)

I am trying to populate two collections with one SQL Server 2008 stored procedure call that return two resultsets. I have "MultipleActiveResultSets=True" in my connection string but I am still getting this error:

'r.NextResult()' threw an exception of type 'System.InvalidOperationException'

Here is my code:

IList<ProjectMember> projectMembers = null;

IList<Project> projects = DbFactory.Run(dbCnx =>
     using (var dbCmd = dbCnx.CreateCommand())
          dbCmd.CommandType = CommandType.StoredProcedure;
          dbCmd.CommandText = "mySchema.myStoredProc";
          dbCmd.Parameters.Add(new SqlParameter("@categoryId", categoryId));

          using (profiler.Step("ProjectService.myStoredProc"))
              var r = dbCmd.ExecuteReader();
              projectMembers = r.ConvertToList<ProjectMember>();
              return r.NextResult() ? r.ConvertToList<Project>() : null;

Is this possible? If so, can someone please show me an example on how to do that?



Accepted Answer

I've found a way but I had to replace ormLite with Dapper:

            using(var cnx = DbFactory.CreateConnection(Global.ConnectionString))
                using (var multi = cnx.QueryMultiple("mySchema.myStoredProc", new { communityId, categoryId }, commandType: CommandType.StoredProcedure))
                    var projectMembers = multi.Read<ProjectMember>().ToList();
                    var projects = multi.Read<Project>().ToList();
                    BindProjectMembers(projects, projectMembers);

                    return projects;

It works perfectly and is twice as fast (from 40 ms to 20 ms) than before the MARS amelioration.

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow