首页 > 代码库 > EF运用成ADO.NET操作返回DataSet或者DataTable

EF运用成ADO.NET操作返回DataSet或者DataTable

     #region 获取DataSet返回结构集
        public DataSet GetDataSet(string sql, params SqlParameter[] para)
        {
            for (int i = 0; i < para.Length; i++)
            {
                SqlParameter p = para[i];
                sql += " " + p.ParameterName + ",";
            }
            sql = sql.Substring(0, sql.Length - 1);


            DataSet sd = new DataSet();
            using (var db = new BaseContext())
            {
                SqlConnection conn = db.Database.Connection as SqlConnection;
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(para);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(sd);
                }
            }
            return sd;
        }
        #endregion

        #region 获取Table返回DataTable
        public DataTable GetDataTable(string sql, params SqlParameter[] para)
        {
            DataTable dt=new DataTable();
            for (int i = 0; i < para.Length; i++)
            {
                SqlParameter p = para[i];
                sql += " " + p.ParameterName + ",";
            }
            sql = sql.Substring(0, sql.Length - 1);

            using (var db = new BaseContext())
            {
                SqlConnection conn = db.Database.Connection as SqlConnection;
                using (SqlCommand cmd=new SqlCommand(sql,conn))
                {
                    cmd.Parameters.AddRange(para);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
        #endregion

 

EF运用成ADO.NET操作返回DataSet或者DataTable