Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with SqlCommandSet not working with Byte Array parameters #1462

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ internal void Append(SqlCommand command)
int offset = p.Offset;
int size = p.Size;
int countOfBytes = byteValues.Length - offset;
if ((0 != size) && (size < countOfBytes))
if ((size > 0) && (size < countOfBytes))
{
countOfBytes = size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.Data.SqlClient.ManualTesting.Tests
{
public static class CommandCancelTest
public static class SqlCommandCancelTest
{
// Shrink the packet size - this should make timeouts more likely
private static readonly string s_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { PacketSize = 512 }).ConnectionString;
Expand Down Expand Up @@ -136,7 +136,7 @@ private static void MultiThreadedCancel(string constr, bool async)

Task.WaitAll(tasks, 15 * 1000);

CommandCancelTest.VerifyConnection(command);
SqlCommandCancelTest.VerifyConnection(command);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Data.SqlTypes;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace System.Data.SqlClient.ManualTesting.Tests
{
public class SqlCommandSetTest
{
private static Assembly sqlclient = Assembly.GetAssembly(typeof(SqlConnection));

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public void TestByteArrayParameters()
{
string tableName = DataTestUtility.GetUniqueNameForSqlServer("CMD");
string procName = DataTestUtility.GetUniqueNameForSqlServer("CMD");
byte[] bArray = new byte[] { 1, 2, 3 };

using (var connection = new SqlConnection(DataTestUtility.TcpConnStr))
{
connection.Open();
try
{
using (var cmd = new SqlCommand(procName, connection))
{
setupByteArrayArtifacts(connection, tableName, procName);

// Insert with SqlCommand
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters["@array"].Value = bArray;

cmd.ExecuteNonQuery();

//Insert with command Set
var commandSetType = sqlclient.GetType("System.Data.SqlClient.SqlCommandSet");
var cmdSet = Activator.CreateInstance(commandSetType, true);
commandSetType.GetMethod("Append", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(cmdSet, new object[] { cmd });
commandSetType.GetProperty("Connection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetSetMethod(true).Invoke(cmdSet, new object[] { connection });
commandSetType.GetMethod("ExecuteNonQuery", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(cmdSet, new object[] { });

cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = $"SELECT * FROM {tableName}";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
SqlBytes byteArray = reader.GetSqlBytes(0);
Assert.Equal(bArray.Length, byteArray.Length);

for (int i = 0; i < bArray.Length; i++)
{
Assert.Equal(bArray[i], byteArray[i]);
}
}

}
}
}
finally
{
dropByteArrayArtifacts(connection, tableName, procName);
}
}
}

private void dropByteArrayArtifacts(SqlConnection connection, string tableName, string procName)
{
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = $"DROP TABLE IF EXISTS {tableName}";
cmd.ExecuteNonQuery();

cmd.CommandText = $"DROP PROCEDURE IF EXISTS {procName}";
cmd.ExecuteNonQuery();
}
}

private void setupByteArrayArtifacts(SqlConnection connection, string tableName, string procName)
{
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = $"CREATE TABLE {tableName} (ByteArrayColumn varbinary(max))";
cmd.ExecuteNonQuery();

cmd.CommandText = $"CREATE PROCEDURE {procName} @array varbinary(max) AS BEGIN SET NOCOUNT ON; " +
$"insert into {tableName}(ByteArrayColumn) values(@array) END";
cmd.ExecuteNonQuery();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<Compile Include="ProviderAgnostic\MultipleResultsTest\MultipleResultsTest.cs" />
<Compile Include="ProviderAgnostic\ReaderTest\ReaderTest.cs" />
<Compile Include="SQL\AsyncTest\AsyncTest.cs" />
<Compile Include="SQL\CommandCancelTest\CommandCancelTest.cs" />
<Compile Include="SQL\SqlCommandTest\SqlCommandCancelTest.cs" />
<Compile Include="SQL\ConnectionPoolTest\ConnectionPoolTest.cs" />
<Compile Include="SQL\ConnectionPoolTest\PoolBlockPeriodTest.netcoreapp.cs" />
<Compile Include="SQL\ConnectivityTests\ConnectivityTest.cs" />
Expand All @@ -61,6 +61,7 @@
<Compile Include="SQL\MirroringTest\ConnectionOnMirroringTest.cs" />
<Compile Include="SQL\ParallelTransactionsTest\ParallelTransactionsTest.cs" />
<Compile Include="SQL\SqlBulkCopyTest\CopyWidenNullInexactNumerics.cs" />
<Compile Include="SQL\SqlCommandTest\SqlCommandSetTest.cs" />
<Compile Include="SQL\SqlCredentialTest\SqlCredentialTest.cs" />
<Compile Include="SQL\SqlSchemaInfoTest\SqlSchemaInfoTest.cs" />
<Compile Include="SQL\SqlBulkCopyTest\AdjustPrecScaleForBulkCopy.cs" />
Expand Down