public static List <T> Binding <T>(this IfxDataReader sqlDataReader) where T : class { var objects = new List <T>(); try { while (sqlDataReader.Read()) { var entity = (T)Activator.CreateInstance(typeof(T)); foreach (var property in typeof(T).GetProperties()) { var value = property.PropertyType.GetDefault(); if (!sqlDataReader.HasColumn(property.Name)) { property.SetValue(entity, value, null); continue; } var index = sqlDataReader.GetOrdinal(property.Name); if (!sqlDataReader.IsDBNull(index)) { var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; value = Convert.ChangeType(sqlDataReader.GetValue(index), type); } property.SetValue(entity, value, null); } objects.Add(entity); } } catch (Exception e) { Console.WriteLine(e); return(null); } return(objects); }
public Search Select(string sqlCount, string sql) { String _connectionString = "Server=ol_svr_custom;" + "Host=localhost;" + "Service=turbo;" + "Database=registration;" + "User ID=informix;" + "Password=123456;" + "Client Locale=ru_ru.CP1251;" + "Database Locale=ru_ru.915;" + "Max Pool Size=500;" + "Pooling=True;" + "Protocol=olsoctcp;" + "Connection Lifetime=1200;" + "Connection Timeout=1;"; try { IfxConnection _Connection = new IfxConnection() { ConnectionString = _connectionString }; _Connection.Open(); try { Search _Search = new Search { Total = "0", ListCitizen = new List <Citizen>() }; IfxCommand _command = new IfxCommand { Connection = _Connection, CommandText = sqlCount }; IfxDataReader _dataReader = _command.ExecuteReader(); while (_dataReader.Read()) { _Search.Total = _dataReader.GetString(0); } _dataReader.Close(); _command.CommandText = sql; _dataReader = _command.ExecuteReader(); while (_dataReader.Read()) { if (!_dataReader.IsDBNull(0)) { Citizen citizen = new Citizen(); citizen.Id = _dataReader.GetString(0); citizen.LastName = _dataReader.GetString(1); citizen.FirstName = _dataReader.GetString(2); citizen.MiddleName = _dataReader.GetString(3); string dt = _dataReader.GetString(4); if (dt.Length == 10) { citizen.DateOfBirth = _dataReader.GetDateTime(4).ToString("d"); } _Search.ListCitizen.Add(citizen); } } if (_dataReader != null) { _dataReader.Close(); } if (_Connection != null) { _Connection.Close(); } return(_Search); } catch { return(null); } } catch { return(null); } }