Исходный код вики Взаимодействие с NpgsqlParameter
Редактировал(а) Alexandr Fokin 2024/12/07 13:48
Скрыть последних авторов
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | | | |
| 2 | | |{{code language="c#"}}public static class QueryParameterExtensions | ||
| 3 | { | ||
| 4 | public static NpgsqlParameter StructToDbParameter<T>( | ||
| 5 | this T value, | ||
| 6 | string name, | ||
| 7 | NpgsqlDbType type | ||
| 8 | ) | ||
| 9 | where T : struct | ||
| 10 | { | ||
| 11 | return new NpgsqlParameter<T>(name, type) { TypedValue = value }; | ||
| 12 | } | ||
| 13 | |||
| 14 | public static NpgsqlParameter StructToDbParameter<T>( | ||
| 15 | this T? value, | ||
| 16 | string name, | ||
| 17 | NpgsqlDbType type | ||
| 18 | ) | ||
| 19 | where T : struct | ||
| 20 | { | ||
| 21 | if (!value.HasValue) | ||
| 22 | { | ||
| 23 | return new NpgsqlParameter(name, type) { Value = DBNull.Value }; | ||
| 24 | } | ||
| 25 | |||
| 26 | return new NpgsqlParameter<T>(name, type) { TypedValue = value.Value }; | ||
| 27 | } | ||
| 28 | |||
| 29 | public static NpgsqlParameter ClassToDbParameter<T>( | ||
| 30 | this T value, | ||
| 31 | string name, | ||
| 32 | NpgsqlDbType type | ||
| 33 | ) | ||
| 34 | where T : class | ||
| 35 | { | ||
| 36 | if (value == null) | ||
| 37 | { | ||
| 38 | return new NpgsqlParameter(name, type) { Value = DBNull.Value }; | ||
| 39 | } | ||
| 40 | |||
| 41 | return new NpgsqlParameter<T>(name, type) { TypedValue = value }; | ||
| 42 | } | ||
| 43 | }{{/code}} | ||
| 44 | | | |