zoukankan      html  css  js  c++  java
  • 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;
                }
            }
    NET新手,希望各位大侠多多指教。
  • 相关阅读:
    AC620教程 第十五节 8位7段数码管驱动设计与验证
    解决NIOS II工程移动在磁盘上位置后project无法编译问题
    基于FPGA的XPT2046触摸控制器设计
    Altera SOPC FrameBuffer系统设计教程
    【小梅哥SOPC学习笔记】SOPC开发常见问题及解决办法集锦
    ChromeDriver的安装和使用
    Selenium的安装和使用
    Requests的安装和使用
    安装python3
    centos安装后的个人工具
  • 原文地址:https://www.cnblogs.com/duanyuerui/p/7098248.html
Copyright © 2011-2022 走看看