首页 > 其他 > 详细

EF 返回DataTable 扩展方法

时间:2017-05-25 00:37:05      阅读:1902      评论:0      收藏:0      [点我收藏+]
    public static class EFExtendMethod
    {
        public static DataTable SqlQueryToDataTable(this Database db, string sql, CommandType type = CommandType.Text, params SqlParameter[] param)
        {
            DataTable ret_dt = new DataTable();
            SqlConnection conn = db.Connection as SqlConnection;
            if (conn == null)
            {
                conn = new SqlConnection(db.Connection.ConnectionString);
            }

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = type;

                if (param != null && param.Length > 0)
                {
                    foreach (SqlParameter p in param)
                    {
                        cmd.Parameters.Add(p);
                    }
                }

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ret_dt);

                conn.Close();
                return ret_dt;
            }
            catch (Exception ex)
            {
                conn.Close();
                return ret_dt;
            }
        }

        public static DataTable SqlQueryToDataTable(this Database db, string sql, params SqlParameter[] param)
        {
            return SqlQueryToDataTable(db,sql,CommandType.Text,param);
        }
    }

  

EF 返回DataTable 扩展方法

原文:http://www.cnblogs.com/linqing/p/6901719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!