• Linq如何执行存储过程


    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() + "'";
            }
        }
    }

  • 相关阅读:
    Neutron LBaaS Service(2)—— Neutron Services Insertion Model
    Gevent工作原理(转)
    异步IO/协程/数据库/队列/缓存(转)
    IO多路复用(转)
    pytz库时区的坑(转)
    Python3.0的新特性(原创)
    Dockerfile 中的 CMD 与 ENTRYPOINT(转)
    RESTful及API设计(原创)
    RESTful服务最佳实践(转)
    Flask restful源码分析(原创)
  • 原文地址:https://www.cnblogs.com/futao/p/2377363.html
Copyright © 2020-2023  润新知