-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix | Fix issues with
SqlCommandSet
not working with Byte Array par…
…ameters (#360)
- Loading branch information
1 parent
5c14f82
commit 8ff5125
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlCommand/SqlCommandSetTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlTypes; | ||
using System.Reflection; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
{ | ||
public class SqlCommandSetTest | ||
{ | ||
private static Assembly mds = Assembly.GetAssembly(typeof(SqlConnection)); | ||
|
||
[CheckConnStrSetupFact] | ||
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.TCPConnectionString)) | ||
using (var cmd = new SqlCommand(procName, connection)) | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
|
||
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 = mds.GetType("Microsoft.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(byteArray.Length, bArray.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(); | ||
} | ||
} | ||
} | ||
} |