Skip to content

Commit aacd971

Browse files
SNOW-1917622 Fix passing null into a query parameter (#1112)
Co-authored-by: VelvetAndrea <[email protected]>
1 parent 36666df commit aacd971

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Snowflake.Data.Tests/IntegrationTests/SFBindTestIT.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void TestArrayBind()
6363
}
6464

6565
[Test]
66-
public void TestBindNullValue()
66+
[TestCaseSource(nameof(NullTestCases))]
67+
public void TestBindNullValue(object nullValue)
6768
{
6869
using (SnowflakeDbConnection dbConnection = new SnowflakeDbConnection())
6970
{
@@ -154,7 +155,7 @@ public void TestBindNullValue()
154155
if (isTypeSupported)
155156
{
156157
command.CommandText = $"insert into {TableName}({colName}) values(:p0)";
157-
param.Value = DBNull.Value;
158+
param.Value = nullValue;
158159
command.Parameters.Add(param);
159160
int rowsInserted = command.ExecuteNonQuery();
160161
Assert.AreEqual(1, rowsInserted);
@@ -164,7 +165,7 @@ public void TestBindNullValue()
164165
try
165166
{
166167
command.CommandText = $"insert into {TableName}(stringData) values(:p0)";
167-
param.Value = DBNull.Value;
168+
param.Value = nullValue;
168169
command.Parameters.Add(param);
169170
int rowsInserted = command.ExecuteNonQuery();
170171
}
@@ -199,6 +200,9 @@ public void TestBindNullValue()
199200
}
200201
}
201202

203+
private static IEnumerable<object> NullTestCases() =>
204+
new object[] { DBNull.Value, null };
205+
202206
[Test]
203207
public void TestBindValue()
204208
{

Snowflake.Data/Client/SnowflakeDbCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb
375375
string bindingType = "";
376376
object bindingVal;
377377

378+
if (parameter.Value == null)
379+
{
380+
parameter.Value = DBNull.Value;
381+
}
382+
378383
if (parameter.Value.GetType().IsArray &&
379384
// byte array and char array will not be treated as array binding
380385
parameter.Value.GetType().GetElementType() != typeof(char) &&

0 commit comments

Comments
 (0)