Verified.
Demo code:
using System;
using System.Data;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
namespace CLRUDT
{
[Serializable]
[SqlUserDefinedType(Format.UserDefined,IsByteOrdered=true,MaxByteSize=255)]
public struct FullName : INullable, IBinarySerialize
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsNull { get; set; }
public static FullName Null
{
get { return new FullName { FirstName = "", LastName = "",
IsNull = true }; }
}
public override string ToString()
{
return IsNull ? "NULL" : string.Format("{0} {1}",
FirstName, LastName);
}
[SqlMethod(OnNullCall = false)]
public static FullName Parse(SqlString s)
{
string[] parts = s.ToString().Split(' ');
return new FullName { FirstName = parts[0], LastName =
parts[1], IsNull = false };
}
public void Read(BinaryReader r)
{
FirstName = r.ReadString();
LastName = r.ReadString();
IsNull = r.ReadBoolean();
}
public void Write(BinaryWriter w)
{
w.Write(FirstName);
w.Write(LastName);
w.Write(IsNull);
}
}
}
and:
using System;
using System.Data.SqlClient;
using CLRUDT;
namespace CLRUDTdemo
{
public class Program
{
public static void Execute(SqlConnection con, string sql)
{
using(SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.ExecuteNonQuery();
}
}
public static void Main(string[] args)
{
using(SqlConnection con = new
SqlConnection(@"Server=ARNEPC4\SQLEXPRESS;Database=Test;Trusted_Connection=True"))
{
con.Open();
Execute(con, string.Format("CREATE ASSEMBLY CLRUDTdll
FROM '{0}' WITH PERMISSION_SET = SAFE",
typeof(FullName).Assembly.Location));
Execute(con, "CREATE TYPE dbo.FullName EXTERNAL NAME
CLRUDTdll.[CLRUDT.FullName]");
Execute(con, "CREATE TABLE demo (id INTEGER NOT NULL
PRIMARY KEY, name FullName)");
Execute(con, "INSERT INTO demo VALUES(1,'Arne Vajhoej')");
using(SqlCommand sel = new SqlCommand("SELECT name FROM
demo WHERE id=1", con))
{
object o = sel.ExecuteScalar();
Console.WriteLine(o.GetType().FullName);
FullName name = (FullName)o;
Console.WriteLine("{0} {1}", name.FirstName,
name.LastName);
}
Execute(con, "DROP TABLE demo");
Execute(con, "DROP TYPE dbo.FullName");
Execute(con, "DROP ASSEMBLY CLRUDTdll");
}
Console.WriteLine("All done");
Console.ReadKey();
}
}
}
Output:
CLRUDT.FullName
Arne Vajhoej
All done
Arne
PS: No - I don't think this is particular relevant for your original
problem. Most likely you just got a nasty
ADO.NET provider. But hey
I have never don a CLR UDR before, so fun!