Allow passing null for SqlParameter value
- Dominant language
- C#
- Stars
- 989
- Forks
- 340
- Avg merge
- 4d 18h
- Merged PRs (30d)
- 69
Description
### Is your feature request related to a problem? Please describe.
Currently, if you pass `null` to the Value in a `SqlParameter` constructor or use the `AddWithValue` method, you receive a rather cryptic exception message::
e.g.
```cs
using var conn = new SqlConnection(...);
await conn.OpenAsync();
using var cmd = new SqlCommand("select @", conn);
cmd.Parameters.Add(new("@", null) { DbType = DbType.Int32 });
var res = await cmd.ExecuteScalarAsync();
```
>Microsoft.Data.SqlClient.SqlException : The parameterized query '(@ int)select @' expects the parameter '@', which was not supplied.
The message is entirely unclear, and makes you have to google it, only to discover that the correct resolution of this problem is to replace `null` with `DbNull.Value`.
There are many, many threads about this on stackoverflow and various forums.
### Describe the solution you'd like
When you create a parameter with `null`, it internally stores down `DbNull.Value`.
This is technically a breaking change, but I'm not sure who actually benefits from this obtuse behavior where null is an exception rather than doing the logical thing.
### Describe alternatives you've considered
### Additional context
Contributor guide
Assessment
This issue has not been assessed yet.