Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

SqlClient Allow specifying a UDT SqlParameter value as bytes #36678

Merged
merged 1 commit into from
Apr 25, 2019
Merged
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
24 changes: 23 additions & 1 deletion src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs
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