Files
LibraryWeb/App_Code/SQLExtensions.cs
2026-08-04 14:41:21 +03:00

154 lines
3.3 KiB
C#

using Microsoft.Data.SqlClient;
using System.Data.SqlTypes;
namespace Library;
public static class SQLExtensions
{
public static string? String( this SqlDataReader reader , string column_id )
{
int column_index = reader.FindColumn( column_id );
if ( column_index < 0 )
return null;
return reader.String( column_index );
}
public static string? String( this SqlDataReader reader , int column_index )
{
if ( reader.IsDBNull( column_index ) )
return null;
string? o = reader[ column_index ].ToString( );
if ( o == null )
return null;
return o!;
}
public static string StringSafe( this SqlDataReader reader , string column_id )
{
int column_index = reader.FindColumn( column_id );
if ( column_index < 0 )
return "";
return reader.StringSafe( column_index );
}
public static string StringSafe( this SqlDataReader reader , int column_index )
{
if ( reader.IsDBNull( column_index ) )
return "";
string? o = reader[ column_index ].ToString( );
if ( o == null ) return "";
return o!;
}
public static DateTime DateTime( this SqlDataReader reader , string column_id )
{
int column_index = reader.FindColumn( column_id );
if ( column_index < 0 )
return System.DateTime.MinValue;
return reader.DateTime( column_index );
}
public static DateTime DateTime( this SqlDataReader reader , int column_index )
{
object data_value = reader[ column_index ];
if ( data_value is DateTime date_time_result )
return date_time_result;
return System.DateTime.MinValue;
}
public static int Int( this SqlDataReader reader , string column_id , int fallback = 0 )
{
int column_index = reader.FindColumn( column_id );
if ( column_index < 0 )
return fallback;
return reader.Int( column_index , fallback );
}
public static int Int( this SqlDataReader reader , int column_index , int fallback = 0 )
{
object data = reader[ column_index ];
if ( data == null )
return fallback;
try
{
return Convert.ToInt32( data );
}
catch
{
}
if ( int.TryParse( data.ToString( ) , out int result ) )
return result;
return fallback;
}
public static long Int64( this SqlDataReader reader , string column_id , long fallback )
{
int column_index = reader.FindColumn( column_id );
if ( column_index < 0 )
return fallback;
return reader.Int64( column_index , fallback );
}
public static long Int64( this SqlDataReader reader , int column_index , long fallback )
{
object data = reader[ column_index ];
if ( data == null )
return fallback;
try
{
return Convert.ToInt64( data );
}
catch
{
}
if ( long.TryParse( data.ToString( ) , out long result ) )
return result;
return fallback;
}
public static decimal Numeric( this SqlDataReader reader , string column_id , decimal fallback )
{
int column_index = reader.FindColumn( column_id );
if ( column_index < 0 )
return fallback;
return reader.Numeric( column_index , fallback );
}
public static decimal Numeric( this SqlDataReader reader , int column_index , decimal fallback )
{
SqlDecimal data = reader.GetSqlDecimal( column_index );
if ( data.IsNull )
return 0;
return data.Value;
}
public static int FindColumn( this SqlDataReader reader , string column_id )
{
try
{
return reader.GetOrdinal( column_id );
}
catch ( Exception )
{
return -1;
}
}
}