zoukankan      html  css  js  c++  java
  • 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() + "'";
            }
        }
    }

  • 相关阅读:
    win10 UWP button
    内网分享资源
    内网分享资源
    CF724F Uniformly Branched Trees
    win10 UWP FlipView
    win10 UWP FlipView
    win10 UWP FlipView
    搭建阿里云 centos mysql tomcat jdk
    搭建阿里云 centos mysql tomcat jdk
    win10 UWP 申请微软开发者
  • 原文地址:https://www.cnblogs.com/futao/p/2377363.html
Copyright © 2011-2022 走看看