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; } } } }
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/