Skip to content

Commit d12d920

Browse files
vkuttypCopilot
andcommitted
feat: add ToClrObject() public method to SqlValue
Returns the CLR-native boxed value (DBNull.Value for Null) for use with DataRow, DataGridView and any code that needs an object without pattern-matching on SqlValue subtypes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0fefa0a commit d12d920

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

src/SqlDotnetty.Core/ISqlDatabase.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static async Task<SqlValue> ScalarAsync(
9494

9595
var dataRow = dt.NewRow();
9696
for (int i = 0; i < row.ColumnCount; i++)
97-
dataRow[i] = SqlValueToClr(row[i]);
97+
dataRow[i] = row[i].ToClrObject();
9898
dt.Rows.Add(dataRow);
9999
}
100100
}
@@ -121,21 +121,5 @@ public static async Task<SqlValue> ScalarAsync(
121121
this ISqlDatabase db, string sql, params SqlParameter[] parameters)
122122
=> db.QueryStreamAsync(sql, parameters).ToDataTableAsync();
123123

124-
private static object SqlValueToClr(SqlValue v) => v switch
125-
{
126-
SqlValue.Null => DBNull.Value,
127-
SqlValue.Bool b => b.Value,
128-
SqlValue.Int8 b => b.Value,
129-
SqlValue.Int16 s => s.Value,
130-
SqlValue.Int32 i => i.Value,
131-
SqlValue.Int64 l => l.Value,
132-
SqlValue.Float f => f.Value,
133-
SqlValue.Double d => d.Value,
134-
SqlValue.Decimal d => d.Value,
135-
SqlValue.Text t => t.Value,
136-
SqlValue.Bytes b => b.Value,
137-
SqlValue.Uuid u => u.Value,
138-
SqlValue.Date d => d.Value,
139-
_ => DBNull.Value,
140-
};
124+
private static object SqlValueToClr(SqlValue v) => v.ToClrObject();
141125
}

src/SqlDotnetty.Core/SqlValue.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ public sealed record Date(DateTime Value) : SqlValue;
102102
public byte[]? AsBytes() => this is Bytes b ? b.Value : null;
103103
public bool IsNull => this is Null;
104104

105+
/// <summary>Returns the CLR-native value for use with DataRow, DataGridView, etc.</summary>
106+
public object ToClrObject() => this switch
107+
{
108+
Null => DBNull.Value,
109+
Bool v => v.Value,
110+
Int8 v => v.Value,
111+
Int16 v => v.Value,
112+
Int32 v => v.Value,
113+
Int64 v => v.Value,
114+
Float v => v.Value,
115+
Double v => v.Value,
116+
Decimal v => v.Value,
117+
Text v => v.Value,
118+
Bytes v => v.Value,
119+
Uuid v => v.Value,
120+
Date v => v.Value,
121+
_ => DBNull.Value,
122+
};
123+
105124
public override string ToString() => this switch
106125
{
107126
Null => "NULL",

0 commit comments

Comments
 (0)