Skip to content

Commit e363121

Browse files
committed
Add the SqlParameterCollection.AddWithValue() method
1 parent b7bf74d commit e363121

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/SqlParameterCollection.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ public static implicit operator SqlParameterCollection(Dictionary<string, object
4848
entry.Value is SqlParameter parameter ? parameter : new SqlParameter(entry.Key, entry.Value)
4949
)];
5050

51+
/// <summary>
52+
/// Adds a new parameter to the end of this collection.
53+
/// </summary>
54+
/// <param name="name">The parameter name.</param>
55+
/// <param name="value">The parameter value.</param>
56+
/// <returns>The newly added parameter.</returns>
57+
public SqlParameter AddWithValue(string name, object? value) {
58+
var parameter = new SqlParameter(name, value);
59+
Add(parameter);
60+
return parameter;
61+
}
62+
5163
/// <summary>
5264
/// Gets a value indicating whether a parameter in this collection has the specified name.
5365
/// </summary>

test/SqlParameterCollection.Tests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace Belin.Sql;
88
[TestClass]
99
public sealed class SqlParameterCollectionTests {
1010

11+
[TestMethod]
12+
public void AddWithValue() {
13+
var collection = new SqlParameterCollection();
14+
IsEmpty(collection);
15+
16+
var parameter = collection.AddWithValue("Name", "Value");
17+
HasCount(1, collection);
18+
AreEqual("@Name", parameter.Name);
19+
AreEqual("Value", parameter.Value);
20+
}
21+
1122
[TestMethod]
1223
public void Constructor() {
1324
// It should create an empty collection by default.

0 commit comments

Comments
 (0)