C/C++教程

SqlQueryDynamic

本文主要是介绍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;
                    }
                }
            }
        }

 

这篇关于SqlQueryDynamic的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!