Skip to content

Commit 6236fe9

Browse files
Fix issues with SqlCommandSet not copying Byte Array parameters correctly
1 parent 18bce3c commit 6236fe9

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommandSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ internal void Append(SqlCommand command)
153153
if (null != byteValues)
154154
{
155155
int offset = p.Offset;
156-
int size = p.Size;
156+
int size = p.GetActualSize();
157157
int countOfBytes = byteValues.Length - offset;
158158
if ((0 != size) && (size < countOfBytes))
159159
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommandSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal void Append(SqlCommand command)
180180
if (null != byteValues)
181181
{
182182
int offset = p.Offset;
183-
int size = p.Size;
183+
int size = p.GetActualSize;
184184
int countOfBytes = byteValues.Length - offset;
185185
if ((0 != size) && (size < countOfBytes))
186186
{

src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTesting.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<Compile Include="SQL\MirroringTest\ConnectionOnMirroringTest.cs" />
114114
<Compile Include="SQL\ParallelTransactionsTest\ParallelTransactionsTest.cs" />
115115
<Compile Include="SQL\SqlBulkCopyTest\CopyWidenNullInexactNumerics.cs" />
116+
<Compile Include="SQL\SqlCommand\SqlCommandSetTest.cs" />
116117
<Compile Include="SQL\SqlCredentialTest\SqlCredentialTest.cs" />
117118
<Compile Include="SQL\SqlSchemaInfoTest\SqlSchemaInfoTest.cs" />
118119
<Compile Include="SQL\SqlStatisticsTest\SqlStatisticsTest.cs" />
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.SqlTypes;
4+
using System.Reflection;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
9+
{
10+
public class SqlCommandSetTest
11+
{
12+
private static Assembly mds = Assembly.GetAssembly(typeof(SqlConnection));
13+
14+
[CheckConnStrSetupFact]
15+
public void TestByteArrayParameters()
16+
{
17+
string tableName = DataTestUtility.GetUniqueName("CMD");
18+
string procName = DataTestUtility.GetUniqueName("CMD");
19+
byte[] bArray = new byte[] { 1, 2, 3 };
20+
21+
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
22+
using (var cmd = new SqlCommand(procName, connection))
23+
{
24+
try
25+
{
26+
connection.Open();
27+
28+
setupByteArrayArtifacts(connection, tableName, procName);
29+
30+
// Insert with SqlCommand
31+
cmd.CommandType = System.Data.CommandType.StoredProcedure;
32+
SqlCommandBuilder.DeriveParameters(cmd);
33+
cmd.Parameters["@array"].Value = bArray;
34+
35+
cmd.ExecuteNonQuery();
36+
37+
//Insert with command Set
38+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
39+
var cmdSet = Activator.CreateInstance(commandSetType, true);
40+
commandSetType.GetMethod("Append", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(cmdSet, new object[] { cmd });
41+
commandSetType.GetProperty("Connection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetSetMethod(true).Invoke(cmdSet, new object[] { connection });
42+
commandSetType.GetMethod("ExecuteNonQuery", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(cmdSet, new object[] { });
43+
44+
cmd.CommandType = System.Data.CommandType.Text;
45+
cmd.CommandText = $"SELECT * FROM {tableName}";
46+
using (SqlDataReader reader = cmd.ExecuteReader())
47+
{
48+
while (reader.Read())
49+
{
50+
SqlBytes byteArray = reader.GetSqlBytes(0);
51+
Assert.Equal(byteArray.Length, bArray.Length);
52+
53+
for (int i = 0; i < bArray.Length; i++)
54+
{
55+
Assert.Equal(bArray[i], byteArray[i]);
56+
}
57+
}
58+
}
59+
}
60+
finally
61+
{
62+
dropByteArrayArtifacts(connection, tableName, procName);
63+
}
64+
}
65+
}
66+
67+
private void dropByteArrayArtifacts(SqlConnection connection, string tableName, string procName)
68+
{
69+
using (SqlCommand cmd = connection.CreateCommand())
70+
{
71+
cmd.CommandText = $"DROP TABLE IF EXISTS {tableName}";
72+
cmd.ExecuteNonQuery();
73+
74+
cmd.CommandText = $"DROP PROCEDURE IF EXISTS {procName}";
75+
cmd.ExecuteNonQuery();
76+
}
77+
}
78+
79+
private void setupByteArrayArtifacts(SqlConnection connection, string tableName, string procName)
80+
{
81+
using (SqlCommand cmd = connection.CreateCommand())
82+
{
83+
cmd.CommandText = $"CREATE TABLE {tableName} (ByteArrayColumn varbinary(max))";
84+
cmd.ExecuteNonQuery();
85+
86+
cmd.CommandText = $"CREATE PROCEDURE {procName} @array varbinary(max) AS BEGIN SET NOCOUNT ON; " +
87+
$"insert into {tableName}(ByteArrayColumn) values(@array) END";
88+
cmd.ExecuteNonQuery();
89+
}
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)