.NET 學習

.NET 學習生活感想... 万事成蹉跎..... 贵在坚持 及时整理自己做过和学过的东西

博客园 首页 新随笔 联系 订阅 管理

 

SqlQueryDynamic
 //调用
            //dynamic dy = PubUtils.SqlQueryDynamic("SELECT * FROM User_Permission");           
            //var upJson = JsonConvert.SerializeObject(dy);
            //var orders = JsonConvert.DeserializeObject<List<SystemManage.UserPermissionModel>>(upJson);

 

public static IEnumerable<dynamic> SqlQueryDynamic(string Sql,params SqlParameter[] parameters)
        {
            string ConnStr = "";
            SqlConnection sqlconn = new SqlConnection(ConnStr == "" ? PubUtils.ConnStr : ConnStr);           
            using (var cmd = sqlconn.CreateCommand())
            {
                cmd.CommandText = Sql;
                if (cmd.Connection.State != ConnectionState.Open)
                {
                    cmd.Connection.Open();
                }

                foreach (var p in parameters)
                {
                    var dbParameter = cmd.CreateParameter();
                    dbParameter.DbType = p.DbType;
                    dbParameter.ParameterName = p.ParameterName;
                    dbParameter.Value = p.Value;
                    cmd.Parameters.Add(dbParameter);
                }

                using (var dataReader = cmd.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        var row = new ExpandoObject() as IDictionary<string, object>;
                        for (var fieldCount = 0; fieldCount < dataReader.FieldCount; fieldCount++)
                        {
                            row.Add(dataReader.GetName(fieldCount), dataReader[fieldCount]);
                        }
                        yield return row;
                    }
                }
            }
        }

 

posted on 2022-07-14 15:51  Tonyyang  阅读(143)  评论(0编辑  收藏  举报
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/