Refactor SQL handling in EnvelopeGenerator application
- Added `System.Data` using directive in `EnvelopeCreateReadSQL.cs`. - Updated SQL command strings to use parameter placeholders. - Corrected method name from `CreateParmas` to `CreateParams` and added output parameter `@OutUid`. - Made similar updates in `EnvelopeReceiverAddReadSQL.cs`. - Introduced `ParamsExtensions` class with `ToSqlParam` method for converting .NET objects to SQL-safe parameter strings.
This commit is contained in:
28
EnvelopeGenerator.Application/SQL/ParamsExtensions.cs
Normal file
28
EnvelopeGenerator.Application/SQL/ParamsExtensions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace EnvelopeGenerator.Application.SQL;
|
||||
|
||||
/// <summary>
|
||||
/// Extension method for converting objects to SQL parameter strings.
|
||||
/// </summary>
|
||||
public static class ParamsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a .NET object to its corresponding SQL-safe parameter string.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to convert.</param>
|
||||
/// <returns>A string representing the SQL parameter.</returns>
|
||||
public static string ToSqlParam(this object? obj)
|
||||
{
|
||||
if (obj is null)
|
||||
return "NULL";
|
||||
else if (obj is string strVal)
|
||||
return $"'{strVal}'";
|
||||
else if (obj is bool boolVal)
|
||||
return boolVal ? "1" : "0";
|
||||
else if (obj is double doubleVal)
|
||||
return $"'{doubleVal}'";
|
||||
else if (obj is int intVal)
|
||||
return intVal.ToString();
|
||||
else
|
||||
throw new NotSupportedException($"Type '{obj.GetType().FullName}' is not supported for SQL parameter conversion.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user