Class Utility

Namespace
DbExport
Assembly
DbExport.Api.dll

A collection of helper methods and properties for database operations, string manipulation, and type conversion within the application.

public static class Utility
Inheritance
Utility
Inherited Members

Properties

Encoding

The default encoding used for reading and writing data within the application. This can be set to a different encoding if needed, but defaults to UTF-8 for broad compatibility with various data sources and formats.

public static Encoding Encoding { get; set; }

Property Value

Encoding

Methods

BinToHex(byte[])

Converts a byte array to its hexadecimal string representation.

public static string BinToHex(byte[] bytes)

Parameters

bytes byte[]

The array of bytes to convert. Cannot be null.

Returns

string

A string containing the hexadecimal values of the input bytes, with each byte represented as a two-digit lowercase hexadecimal number.

Remarks

The resulting string concatenates the hexadecimal representations of each byte in the order they appear in the array. This method is useful for displaying binary data in a readable format, such as for logging or serialization.

EmptyDictionary<TValue>()

Creates and returns an empty dictionary with string keys that uses a case-insensitive string comparer.

public static Dictionary<string, TValue> EmptyDictionary<TValue>()

Returns

Dictionary<string, TValue>

An empty dictionary with string keys and values of type TValue. The dictionary uses a case-insensitive comparer for its keys.

Type Parameters

TValue

The type of the values stored in the dictionary.

Remarks

This method is useful for scenarios where a default empty dictionary is needed with case-insensitive key comparison. The returned dictionary can be used as a placeholder or default value without incurring additional instantiation overhead.

Escape(string, string)

Escapes the specified identifier name according to the syntax conventions of the given database provider.

public static string Escape(string name, string providerName)

Parameters

name string

The identifier name to be escaped, such as a table or column name.

providerName string

The name of the database provider that determines the escaping format to use.

Returns

string

A string containing the escaped identifier, formatted according to the requirements of the specified database provider.

Remarks

For ACCESS, SQLSERVER, and SQLITE providers, the identifier is enclosed in square brackets. For MYSQL, it is enclosed in backticks. For any other provider, the identifier is enclosed in double quotes.

FromBaseN(string, byte)

Converts a string representation of a number in a specified base back to its byte value.

public static byte FromBaseN(string value, byte n)

Parameters

value string

The string representation of the number to convert. It should consist of valid digits for the specified base.

n byte

The base of the number system used in the input string. Must be between 2 and 36, inclusive.

Returns

byte

A byte value corresponding to the input string interpreted as a number in the specified base. Returns 0 if the input string is empty or invalid.

FromBitString(string)

Converts a binary string representation into an array of bytes.

public static byte[] FromBitString(string value)

Parameters

value string

The binary string to convert, which must consist of '0' and '1' characters. The string will be padded with leading zeros to ensure its length is a multiple of 8.

Returns

byte[]

An array of bytes representing the binary string. Each byte corresponds to 8 bits from the input string.

Remarks

The input string must only contain '0' and '1' characters. If the input string is shorter than a multiple of 8, it will be padded with leading zeros. This method is useful for converting binary data represented as a string into a byte array for further processing.

GetConnection(Database)

Creates and returns a database connection using the specified database configuration.

public static DbConnection GetConnection(Database database)

Parameters

database Database

The database object containing the provider name and connection string used to establish the connection. Cannot be null.

Returns

DbConnection

A DbConnection instance representing the established connection to the database.

Remarks

The connection is created based on the provider name and connection string provided by the database object. Ensure that the database object is properly configured before calling this method.

GetConnection(string, string)

Creates and returns a database connection using the specified provider name and connection string.

public static DbConnection GetConnection(string providerName, string connectionString)

Parameters

providerName string

The name of the database provider to use for creating the connection. This must match a registered provider name.

connectionString string

The connection string used to establish the connection to the database. It must be a valid connection string for the specified provider.

Returns

DbConnection

A DbConnection object that represents the established connection to the database.

Remarks

Ensure that the provider name is valid and that the connection string is correctly formatted for the provider being used.

Exceptions

InvalidOperationException

Thrown if a connection cannot be created for the specified provider name.

IsBoolean(object, out bool)

Determines whether the specified object can be successfully converted to a boolean value.

public static bool IsBoolean(object value, out bool converted)

Parameters

value object

The object to evaluate as a potential boolean value.

converted bool

When this method returns, contains the boolean value equivalent of the input, if the conversion was successful; otherwise, false.

Returns

bool

True if the input object can be converted to a boolean value; otherwise, false.

IsDate(object, out DateTime)

Determines whether the specified object can be interpreted as a date/time value.

public static bool IsDate(object value, out DateTime converted)

Parameters

value object

The object to evaluate for date/time representation.

converted DateTime

When this method returns, contains the DateTime representation of the value if conversion is successful; otherwise, it is set to DateTime.Min.

Returns

bool

true if the value can be successfully converted to a date/time type; otherwise, false.

IsEmpty(object)

Determines whether the specified object is considered empty.

public static bool IsEmpty(object value)

Parameters

value object

The object to evaluate for emptiness. This can be null, a string, or an array.

Returns

bool

Returns true if the object is null, DBNull, an empty string, or an empty array; otherwise, returns false.

Remarks

This method is useful for validating input values before processing. It handles various types of objects, providing a consistent way to check for emptiness.

IsNumeric(object, out decimal)

Determines whether the specified object can be interpreted as a numeric value.

public static bool IsNumeric(object value, out decimal converted)

Parameters

value object

The object to evaluate for numeric representation.

converted decimal

When this method returns, contains the decimal representation of the value if conversion is successful; otherwise, it is set to zero.

Returns

bool

true if the value can be successfully converted to a numeric type; otherwise, false.

ParseConnectionString(string)

Parses a connection string into a dictionary of key-value pairs.

public static Dictionary<string, string> ParseConnectionString(string connectionString)

Parameters

connectionString string

The connection string to be parsed, formatted as key-value pairs separated by semicolons.

Returns

Dictionary<string, string>

A dictionary containing the parsed key-value pairs from the connection string. Each key corresponds to a setting name, and each value corresponds to the setting's value.

Remarks

If a key is present without a corresponding value, it will be stored with an empty string as its value. Keys are trimmed of whitespace, and values are unquoted if they are enclosed in double quotes.

QuotedStr(object, char)

Formats the specified value as a string enclosed in the specified quote character, escaping any existing quotes within the value.

public static string QuotedStr(object value, char quote = '\'')

Parameters

value object

The value to be formatted as a quoted string. This can be any object that can be converted to a string.

quote char

The character used to enclose the value. The default is a single quote (').

Returns

string

A string that represents the value enclosed in the specified quote character, with any existing quotes in the value escaped.

Remarks

If the value is null, it will be treated as an empty string. The method replaces any occurrences of the quote character within the value with two instances of that character to ensure proper formatting.

RegisterDbProviderFactories()

Registers database provider factories for supported database types, enabling ADO.NET support for those providers within the application.

public static void RegisterDbProviderFactories()

SanitizeConnectionString(string)

Returns a sanitized version of the specified connection string with sensitive information, such as passwords, masked to prevent exposure.

public static string SanitizeConnectionString(string connectionString)

Parameters

connectionString string

The connection string to be sanitized. May contain sensitive information that should be masked before logging or displaying.

Returns

string

A connection string in which password values are replaced with asterisks of the same length as the original value.

Remarks

This method identifies password-related keys in the connection string using a regular expression and masks their values. Use this method to safely display or log connection strings without revealing sensitive credentials.

Split(string, char)

Splits the specified string into an array of substrings based on the provided separator character, removing empty entries and trimming whitespace from each substring.

public static string[] Split(string input, char separator)

Parameters

input string

The string to be split into substrings. Cannot be null.

separator char

The character that delimits the substrings in the input string.

Returns

string[]

An array of strings containing the substrings from the input string. The array will be empty if the input string is null, empty, or contains only separator characters.

Remarks

This method uses StringSplitOptions.RemoveEmptyEntries and StringSplitOptions.TrimEntries to ensure that the resulting array contains only non-empty, trimmed substrings.

ToBaseN(byte, byte)

Converts the specified byte value to its string representation in the given base.

public static string ToBaseN(byte b, byte n)

Parameters

b byte

The byte value to convert.

n byte

The base for the conversion. Must be between 2 and 36, inclusive.

Returns

string

A string representing the byte value in the specified base. Returns "0" if the input value is zero.

Remarks

Supports bases from 2 to 36, using alphanumeric characters for digits beyond 9.

ToBitString(byte[])

Converts an array of bytes to a single binary string representation, where each byte is represented as an 8-character binary value.

public static string ToBitString(byte[] bytes)

Parameters

bytes byte[]

The array of bytes to convert. Cannot be null.

Returns

string

A string containing the binary representation of the input byte array, with each byte's binary value concatenated in order.

Remarks

The resulting string does not include separators between bytes. Each byte is represented as an 8-character binary string, preserving leading zeros.

ToByte(object)

Converts the specified object to its equivalent byte value, if possible.

public static byte ToByte(object value)

Parameters

value object

The object to convert.

Returns

byte

The byte value of the specified object if the conversion succeeds; otherwise, 0.

ToInt16(object)

Converts the specified object to its equivalent 16-bit signed integer using the current culture's formatting conventions.

public static short ToInt16(object value)

Parameters

value object

The object to convert.

Returns

short

A 16-bit signed integer equivalent to the numeric value contained in the specified object, or zero if the conversion is unsuccessful.

ToParameterName(string, string)

Converts a column name into a parameter name formatted according to the conventions of the specified database provider.

public static string ToParameterName(string columnName, string providerName)

Parameters

columnName string

The name of the column to be converted into a parameter name.

providerName string

The name of the database provider that determines the parameter name format.

Returns

string

A string representing the formatted parameter name suitable for the specified database provider.

TransformConnectionString(string, Func<string, string, string>)

Transforms the specified connection string by applying a provided transformation function to each property value.

public static string TransformConnectionString(string connectionString, Func<string, string, string> transformer)

Parameters

connectionString string

The connection string to be transformed, formatted as a semicolon-separated list of key-value pairs.

transformer Func<string, string, string>

A function that takes a property key and its corresponding value, and returns the transformed value for that property.

Returns

string

A new connection string with the transformed property values, formatted as a semicolon-separated list.

Remarks

The method parses the input connection string into its constituent properties before applying the transformation function to each value.

UnquotedStr(object, char)

Removes surrounding quote characters from the specified string representation and replaces any consecutive quote characters with a single instance.

public static string UnquotedStr(object value, char quote = '\'')

Parameters

value object

The object to process. The object's string representation must contain surrounding quote characters. Cannot be null.

quote char

The character used to denote quotes in the string. Defaults to a single quote (').

Returns

string

A string with the surrounding quote characters removed and any consecutive quote characters replaced with a single instance. Returns an empty string if the input is null or does not contain surrounding quotes.

Remarks

This method is useful for sanitizing input strings that may have been quoted for formatting purposes. It assumes that the input string is well-formed and contains at least one character.