using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace XXXX.Data.Helper
{
    public static class DatabaseExtension
    {
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="sp"></param>
        /// <param name="parameters">new SqlParameter() </param>
        /// <returns></returns>
        public static int ProcedureQuery(string sp, params SqlParameter[] parameters)
        {
            using (var context = new XXXXDbContext())
            {
                using (context.Database.Connection)
                {
                    context.Database.Connection.Open();
                    var cmd = context.Database.Connection.CreateCommand();
                    cmd.CommandText = sp;
                    cmd.CommandType = CommandType.StoredProcedure;
                    foreach (var param in parameters)
                    {
                        cmd.Parameters.Add(param);
                    }
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        /// <summary>
        /// 根据存储过程查询所需要的数据
        /// </summary>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="sp"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static IEnumerable<TElement> ProcedureQuery<TElement>(string sp, params object[] parameters)
        {
            string sql = "";
            List<TElement> items = new List<TElement>();
            using (var context = new XXXXDbContext())
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    sql += "" + FormatSqlString(parameters[i]) + ",";
                }
                sql = "EXEC " + sp + " " + sql.TrimEnd(',');
                var tempResult = context.Database.SqlQuery<TElement>(sql, parameters);
                if (tempResult != null && tempResult.Count() > 0)
                {
                    items.AddRange(tempResult);
                }
            }
            return items;
        }
        public static IEnumerable<TElement> ProcedureQuery<TElement>(string sp)
        {
            string sql = "";
            List<TElement> items = new List<TElement>();
            using (var context = new XXXXDbContext())
            {
               
                sql = "EXEC " + sp + " ";
                var tempResult = context.Database.SqlQuery<TElement>(sql);
                if (tempResult != null && tempResult.Count() > 0)
                {
                    items.AddRange(tempResult);
                }
            }
            return items;
        }
        private static string FormatSqlString(object obj)
        {
            if (obj.GetType() == typeof(System.Boolean))
            {
                return ((bool)obj ? "1" : "0");
            }
            if (obj.GetType() == typeof(System.Guid))
            {
                return "'" + obj.ToString() + "'";
            }
            if (obj.GetType() == typeof(System.Int16) || obj.GetType() == typeof(System.Int32) || obj.GetType() == typeof(System.Int64) || obj.GetType() == typeof(System.Decimal) || obj.GetType() == typeof(System.Double))
            {
                return obj.ToString();
            }
            return "'" + obj.ToString() + "'";
        }
    }
}

posted on 2012-03-02 16:15  baby_tao  阅读(5801)  评论(0编辑  收藏  举报