Skip to content

Commit

Permalink
Merge pull request dotnet/corefx#36678 from Wraith2/sqlfeat-udtbytes
Browse files Browse the repository at this point in the history
SqlClient Allow specifying a UDT SqlParameter value as bytes

Commit migrated from dotnet/corefx@aff33dc
  • Loading branch information
AfsanehR-zz authored Apr 25, 2019
2 parents ca2ccc3 + e9ea388 commit 5084ed4
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7489,7 +7489,29 @@ private Task TDSExecuteRPCAddParameter(TdsParserStateObject stateObj, SqlParamet

if (!isNull)
{
udtVal = _connHandler.Connection.GetBytes(value, out format, out maxsize);
if (value is byte[] rawBytes)
{
udtVal = rawBytes;
}
else if (value is SqlBytes sqlBytes)
{
switch (sqlBytes.Storage)
{
case StorageState.Buffer:
// use the buffer directly, the only way to create it is with the correctly sized byte array
udtVal = sqlBytes.Buffer;
break;
case StorageState.Stream:
case StorageState.UnmanagedBuffer:
// allocate a new byte array to store the data
udtVal = sqlBytes.Value;
break;
}
}
else
{
udtVal = _connHandler.Connection.GetBytes(value, out format, out maxsize);
}

Debug.Assert(null != udtVal, "GetBytes returned null instance. Make sure that it always returns non-null value");
size = udtVal.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,74 @@ public static void TestUdtSchemaMetadata()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void TestUdtParameterSetSqlByteValue()
{
const string ExpectedPointValue = "POINT (1 1)";
SqlBytes geometrySqlBytes = null;
string actualtPointValue = null;

using (SqlConnection connection = new SqlConnection(DataTestUtility.TcpConnStr))
{
connection.Open();

using (var command = connection.CreateCommand())
{
command.CommandText = $"SELECT geometry::Parse('{ExpectedPointValue}')";
using (var reader = command.ExecuteReader())
{
reader.Read();
geometrySqlBytes = reader.GetSqlBytes(0);
}
}

using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT @geometry.STAsText()";
var parameter = command.Parameters.AddWithValue("@geometry", geometrySqlBytes);
parameter.SqlDbType = SqlDbType.Udt;
parameter.UdtTypeName = "geometry";
actualtPointValue = Convert.ToString(command.ExecuteScalar());
}

Assert.Equal(ExpectedPointValue, actualtPointValue);
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void TestUdtParameterSetRawByteValue()
{
const string ExpectedPointValue = "POINT (1 1)";
byte[] geometryBytes = null;
string actualtPointValue = null;

using (SqlConnection connection = new SqlConnection(DataTestUtility.TcpConnStr))
{
connection.Open();

using (var command = connection.CreateCommand())
{
command.CommandText = $"SELECT geometry::Parse('{ExpectedPointValue}')";
using (var reader = command.ExecuteReader())
{
reader.Read();
geometryBytes = reader.GetSqlBytes(0).Buffer;
}
}

using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT @geometry.STAsText()";
var parameter = command.Parameters.AddWithValue("@geometry", geometryBytes);
parameter.SqlDbType = SqlDbType.Udt;
parameter.UdtTypeName = "geometry";
actualtPointValue = Convert.ToString(command.ExecuteScalar());
}

Assert.Equal(ExpectedPointValue, actualtPointValue);
}
}

private static void AssertSqlUdtAssemblyQualifiedName(string assemblyQualifiedName, string expectedType)
{
List<string> parts = assemblyQualifiedName.Split(',').Select(x => x.Trim()).ToList();
Expand Down

0 comments on commit 5084ed4

Please sign in to comment.