| 
 | 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