码迷,mamicode.com
首页 > 数据库 > 详细

EF中的MySql返回 DataTable公共类库

时间:2016-08-23 11:29:09      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

public static class SqlHelper
    {
        /// <summary>
        /// EF SQL 语句返回 dataTable
        /// </summary>
        /// <param name="db">ef数据上下文</param>
        /// <param name="sql">语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static DataTable SqlQueryForDataTatable(this Database db, string sql, SqlParameter[] parameters=null)
        {
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = db.Connection.ConnectionString;
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            if (parameters!=null && parameters.Length > 0)
            {
                foreach (var item in parameters)
                {
                    cmd.Parameters.Add(item);
                }
            }
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            return table;
        }
    }

  

EF中的MySql返回 DataTable公共类库

标签:

原文地址:http://www.cnblogs.com/qinyi173/p/5798337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!