EF 4.1代码第一个多个结果集 [英] EF 4.1 Code First Multiple Result Sets

查看:89
本文介绍了EF 4.1代码第一个多个结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行返回多个结果集的Raw SQL查询。这是可能的EF CF?

I need to execute a Raw SQL query that returns multiple result sets. Is that possible in EF CF ?

谢谢!

推荐答案

说明



是的,你可以! ;)您可以使用实体框架代码中的DbCommand执行原始sql查询。

Description

Yes you can! ;) You can perform a raw sql query using DbCommand in Entity Framework Code First too.

可以使用多个结果集执行查询,并使用 SqlDataReader 类的NextResult()方法

You can perform queries with multiple result sets and jump to the next result set using NextResult() method of the SqlDataReader class.

namespace MyNamespace
{
    public class MyDbContext : DbContext
    {

    }

    public class Test
    {
        public void Test()
        {
            MyDbContext context = new MyDbContext();

            DbCommand db = context.Database.Connection.CreateCommand();
            db.CommandText = "SELECT propertie1 FROM Table1; SELECT propertie1 from Table2";
            try
            {
                DbDataReader reader = db.ExecuteReader();

                while (reader.Read())
                {
                    // read your data from result set 1
                    string value = Convert.ToString(reader["propertie1"]);
                }

                reader.NextResult();

                while (reader.Read())
                {
                    // read your data from result set 2
                    string value = Convert.ToString(reader["propertie1"]);
                }
            }
            catch
            {
                // Exception handling
            }
            finally
            {
                if (db.Connection.State != ConnectionState.Closed)
                    db.Connection.Close();
            }
        }
    }
}



更多信息




  • 在EF 4.1中使用DbContext第10部分:原始SQL查询

  • More Information

    • Using DbContext in EF 4.1 Part 10: Raw SQL Queries
    • 这篇关于EF 4.1代码第一个多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆