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

  • 相关阅读:
    AsyncTask(异步任务)
    Android之listview添加数据篇
    Android之ListView动态添加数据(SQLiteOpenHelper类添加数据)
    Android之sqlite数据库版本升级和降级的处理(onUpgrade和onDowngrade)
    Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机
    Android之SQLite
    Eclipse快捷键
    Android之微信布局篇
    Android之MainActivity类
    onOptionsItemSelected、onMenuItemSelected、onContextItemSelected 区别
  • 原文地址:https://www.cnblogs.com/futao/p/2377363.html
Copyright © 2011-2022 走看看