Visual Studio - absolutnie prosty generator klasy DTO z jednej jedynej tabeli

0

Jest sobie tabela w MS-SQL, tak szeroka, że światło przygasa jak ją prawo-lewo przewijam (nie moja, taką otrzymałem).
trzeba mi z tego mikro encję zrobić (użyłem w tytule DTO, żeby nie straszyć), po prostu 40-50 properties.

Jak wiadomo, lenistwo jest motorem postępu. Jak wygenerować klasę z bazy (tabeli)

Nie ma żadnego frameworka do bazy, nie ma EF, skrajny minimalizm.

Na znacznie węższej tabeli mam ręcznie zrobioną prowizorkę:

public class WierszModelu2
    {

        public DateTime Data { get; internal set; }

        public string Pesel { get; internal set; }
        public int IdOrig { get; internal set; }
        public string NumerUmowy { get; internal set; }
        public string Opis { get; internal set; }
        public string NazwiskoImie { get; internal set; }

        public WierszModelu2(SqlDataReader reader)
        {
            Data = reader.GetDateTime(reader.GetOrdinal("data_eksportu"));
            Pesel = reader.GetString(reader.GetOrdinal("pesel"));
            IdOrig = reader.GetInt32(reader.GetOrdinal("id_danych"));
            Opis = reader.GetString(reader.GetOrdinal("opis"));
            NumerUmowy = reader.GetString(reader.GetOrdinal("numer_um"));
            NazwiskoImie = reader.GetString(reader.GetOrdinal("nazwisko_imie"));

        }
    }

Edit: nie krzyczcie za język polski. Dziedzina jest nieprzekładalna na angielski.

4

W ms sql można zrobić selecta i zwrócić jako JSON potem wklejam to w Visual Studio jako klasę.

https://www.uslugi-komputerowe.eu/internet/jak-szybko-stworzyc-klase-z-sql-server-w-c/

1
Damian Korczowski napisał(a):

W ms sql można zrobić selecta i zwrócić jako JSON potem wklejam to w Visual Studio jako klasę.

https://www.uslugi-komputerowe.eu/internet/jak-szybko-stworzyc-klase-z-sql-server-w-c/

Nie znałem tych opcji, dzięki.
partia Wam tego nie zapomni.

0

Hura!!! Jestem leniwy !!!

Odwdzięczam się konstruktorem do klasy w/g receptury @Damian Korczowski
Część w komentarzach (wg typu property) być może jest rozwojowa, ale typy liczbowe mnie kopały.
Może się komuś przyda.

UWAGA: w procedurze Damiana każda pole liczbowe nie będące integerem, zostaje generowane w klasie jako float (słabość JSON-a), trzeba sobie opanować wg siebie. Moje "opanowanie" to decimal, i charakterystyczne podobne potraktowanie bazodanowego "Single" i "Decimal".
Też się kurcze twórcy chciało podatki liczyć na floatach ...

Druga moja ingerencja w wygenerowaną klasę, to zmiana set; -> internal set;

public WierszModeluAbc(SqlDataReader reader)
        {
            foreach (PropertyInfo pr in this.GetType().GetProperties())
            {
                int key = reader.GetOrdinal(pr.Name);

                if (!reader.IsDBNull(key)) {
                    switch (reader.GetFieldType(key).Name)
                    {
                        case "Int32": pr.SetValue(this, reader.GetInt32(key), null); break;
                        case "Int16": pr.SetValue(this, reader.GetInt16(key), null); break;
                        case "Int64": pr.SetValue(this, reader.GetInt64(key), null); break;
                        case "String": pr.SetValue(this, reader.GetString(key), null); break;
                        case "DateTime": pr.SetValue(this, reader.GetDateTime(key), null); break;
                        case "Single": pr.SetValue(this, (decimal) reader.GetFloat(key), null); break;
                        case "Decimal": pr.SetValue(this, reader.GetDecimal(key), null); break;

                        default:
                            string sss = reader.GetFieldType(key).Name;
                            int ccc = 1;
                            break;
                    }
                  /*  switch (pr.PropertyType.Name) {
                        case "String": pr.SetValue(this, reader.GetString(key), null); break;
                        case "Int32": pr.SetValue(this, reader.GetInt32(key), null); break;
                        case "Int16": pr.SetValue(this, reader.GetInt16(key), null); break;
                        case "Int64": pr.SetValue(this, reader.GetInt64(key), null); break;
                        // case "Single": pr.SetValue(this, reader.GetFloat(key), null); break;
                        case "Decimal": pr.SetValue(this, reader.GetDecimal(key), null); break;
                        //   case "Double": pr.SetValue(this, reader.GetDouble(key), null); break;
                        case "DateTime": pr.SetValue(this, reader.GetDateTime(key), null); break;
                        default:
                            string sss = pr.PropertyType.Name;
                            int ccc = 1;
                            break;

                    } */
                }
            }
        }
1

Kiedyś znalazłem takie coś. Może się przyda :)

declare @TableName sysname = '[dbo].[vwKlienci]'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'

select @Result = @Result + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'
from
(
    select 
        replace(col.name, ' ', '_') ColumnName,
        column_id ColumnId,
        case typ.name 
            when 'bigint' then 'long'
            when 'binary' then 'byte[]'
            when 'bit' then 'bool'
            when 'char' then 'string'
            when 'date' then 'DateTime'
            when 'datetime' then 'DateTime'
            when 'datetime2' then 'DateTime'
            when 'datetimeoffset' then 'DateTimeOffset'
            when 'decimal' then 'decimal'
            when 'float' then 'double'
            when 'image' then 'byte[]'
            when 'int' then 'int'
            when 'money' then 'decimal'
            when 'nchar' then 'string'
            when 'ntext' then 'string'
            when 'numeric' then 'decimal'
            when 'nvarchar' then 'string'
            when 'real' then 'double'
            when 'smalldatetime' then 'DateTime'
            when 'smallint' then 'short'
            when 'smallmoney' then 'decimal'
            when 'text' then 'string'
            when 'time' then 'TimeSpan'
            when 'timestamp' then 'DateTime'
            when 'tinyint' then 'byte'
            when 'uniqueidentifier' then 'Guid'
            when 'varbinary' then 'byte[]'
            when 'varchar' then 'string'
            else 'UNKNOWN_' + typ.name
        end ColumnType,
        case 
            when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier') 
            then '?' 
            else '' 
        end NullableSign
    from sys.columns col
        join sys.types typ on
            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
    where object_id = object_id(@TableName)
) t
order by ColumnId

set @Result = @Result  + '
}'

print @Result

1 użytkowników online, w tym zalogowanych: 0, gości: 1