EF执行存储过程返回DataSet

Posted 秋明小司机丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF执行存储过程返回DataSet相关的知识,希望对你有一定的参考价值。

 public static System.Collections.Generic.IEnumerable<TElement> RunSP<TElement>(this Database self, string spName, params SqlParameter[] paras)
        {
            System.Collections.Generic.IEnumerable<TElement> entityList = new System.Collections.Generic.List<TElement>();
            DataSet set = self.RunSP(spName, paras);
            if ((set != null) && (set.Tables.get_Count() >= 1))
            {
                entityList = EntityHelper.GetEntityList<TElement>(set.Tables.get_Item(0));
            }
            return entityList;
        }
        
        public static DataSet RunSP(this Database self, string spName, params SqlParameter[] paras)
        {
            if (paras != null)
            {
                SqlParameter[] parameterArray = paras;
                for (int i = 0; i < parameterArray.Length; i = (int) (i + 1))
                {
                    SqlParameter parameter = parameterArray[i];
                    if (parameter.get_Value() == null)
                    {
                        parameter.set_Value(System.DBNull.Value);
                    }
                }
            }
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlConnection connection = self.Connection as SqlConnection;
            using (SqlCommand command = new SqlCommand(spName, connection))
            {
                adapter.set_SelectCommand(command);
                command.set_CommandType(CommandType.StoredProcedure);
                if (paras != null)
                {
                    SqlParameter[] parameterArray2 = paras;
                    for (int j = 0; j < parameterArray2.Length; j = (int) (j + 1))
                    {
                        SqlParameter parameter2 = parameterArray2[j];
                        command.Parameters.Add(parameter2);
                    }
                }
                DataSet set = new DataSet();
                adapter.Fill(set);
                adapter.SelectCommand.Parameters.Clear();
                return set;
            }
        }
        
        public static DataSet RunSP(this Database self, string spName, SqlTransaction tran, params SqlParameter[] paras)
        {
            if (paras != null)
            {
                SqlParameter[] parameterArray = paras;
                for (int i = 0; i < parameterArray.Length; i = (int) (i + 1))
                {
                    SqlParameter parameter = parameterArray[i];
                    if (parameter.get_Value() == null)
                    {
                        parameter.set_Value(System.DBNull.Value);
                    }
                }
            }
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlConnection connection = self.Connection as SqlConnection;
            using (SqlCommand command = new SqlCommand(spName, connection))
            {
                if (tran != null)
                {
                    command.set_Transaction(tran);
                }
                adapter.set_SelectCommand(command);
                command.set_CommandType(CommandType.StoredProcedure);
                if (paras != null)
                {
                    SqlParameter[] parameterArray2 = paras;
                    for (int j = 0; j < parameterArray2.Length; j = (int) (j + 1))
                    {
                        SqlParameter parameter2 = parameterArray2[j];
                        command.Parameters.Add(parameter2);
                    }
                }
                DataSet set = new DataSet();
                adapter.Fill(set);
                adapter.SelectCommand.Parameters.Clear();
                return set;
            }
        }
        
        public static System.Collections.Generic.IEnumerable<TElement> RunSql<TElement>(this Database self, string command, params SqlParameter[] paras)
        {
            System.Collections.Generic.IEnumerable<TElement> entityList = new System.Collections.Generic.List<TElement>();
            DataSet set = self.RunSql(command, paras);
            if ((set != null) && (set.Tables.get_Count() >= 1))
            {
                entityList = EntityHelper.GetEntityList<TElement>(set.Tables.get_Item(0));
            }
            return entityList;
        }
        
        public static DataSet RunSql(this Database self, string command, params SqlParameter[] paras)
        {
            if (paras != null)
            {
                SqlParameter[] parameterArray = paras;
                for (int i = 0; i < parameterArray.Length; i = (int) (i + 1))
                {
                    SqlParameter parameter = parameterArray[i];
                    if (parameter.get_Value() == null)
                    {
                        parameter.set_Value(System.DBNull.Value);
                    }
                }
            }
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlConnection connection = self.Connection as SqlConnection;
            using (SqlCommand command2 = new SqlCommand(command, connection))
            {
                command2.set_CommandType(CommandType.Text);
                if (paras != null)
                {
                    SqlParameter[] parameterArray2 = paras;
                    for (int j = 0; j < parameterArray2.Length; j = (int) (j + 1))
                    {
                        SqlParameter parameter2 = parameterArray2[j];
                        command2.Parameters.Add(parameter2);
                    }
                }
                adapter.set_SelectCommand(command2);
                DataSet set = new DataSet();
                adapter.Fill(set);
                adapter.SelectCommand.Parameters.Clear();
                return set;
            }
        }
        
        public static DataSet RunSql(this Database self, string command, SqlTransaction tran, params SqlParameter[] paras)
        {
            if (paras != null)
            {
                SqlParameter[] parameterArray = paras;
                for (int i = 0; i < parameterArray.Length; i = (int) (i + 1))
                {
                    SqlParameter parameter = parameterArray[i];
                    if (parameter.get_Value() == null)
                    {
                        parameter.set_Value(System.DBNull.Value);
                    }
                }
            }
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlConnection connection = self.Connection as SqlConnection;
            using (SqlCommand command2 = new SqlCommand(command, connection))
            {
                if (tran != null)
                {
                    command2.set_Transaction(tran);
                }
                command2.set_CommandType(CommandType.Text);
                if (paras != null)
                {
                    SqlParameter[] parameterArray2 = paras;
                    for (int j = 0; j < parameterArray2.Length; j = (int) (j + 1))
                    {
                        SqlParameter parameter2 = parameterArray2[j];
                        command2.Parameters.Add(parameter2);
                    }
                }
                adapter.set_SelectCommand(command2);
                DataSet set = new DataSet();
                adapter.Fill(set);
                adapter.SelectCommand.Parameters.Clear();
                return set;
            }
        }

 

以上是关于EF执行存储过程返回DataSet的主要内容,如果未能解决你的问题,请参考以下文章

oracle 存储过程执行动态SQL 返回结果给游标,外部程序获得dataset结果集。

关于EF执行返回表的存储过程

sql存储过程,根据多个条件查询,返回一个dataTable或者DataSet

动态存储过程和 EF4.1

EF执行SQL语句和存储过程

C#,EF中执行存储过程返回一个ObjectResult对象,这个对象是延迟加载的吗?