SqlQueryDynamic


 

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;
                    }
                }
            }
        }

 

原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/274372.html

(0)
上一篇 2022年7月14日
下一篇 2022年7月14日

相关推荐

发表回复

登录后才能评论