我是 CLR UDT 的新手,我认为在大多数情况下我还可以,但我目前有一个非常烦人的问题,即行没有显示正确的值。我有一个性别数据类型并输入“男性”或“女性”,但结果窗口中的值分别显示为“0x044D616C65”和“0x0646656D616C65”。这是我的UDT:
[Serializable]
[SqlUserDefinedType(Format.UserDefined, IsByteOrdered = true, MaxByteSize = 512)]
public struct Gender : INullable, IBinarySerialize
{
private bool m_Null;
private SqlString genderName;
public override string ToString()
{
return Convert.ToString(genderName);
}
public bool IsNull
{
get { return m_Null; }
}
public static Gender Null
{
get
{
Gender h = new Gender();
h.m_Null = true;
return h;
}
}
public static Gender Parse(SqlString s)
{
if (s.IsNull)
{
return Null;
}
Gender u = new Gender();
bool isValid = false;
string str = s.ToString().Trim();
if (str.StartsWith("M") || str.StartsWith("F"))
{
isValid = true;
}
if (isValid)
{
u.genderName = new SqlString(str);
}
else
{
throw new SqlTypeException("Gender not valid");
}
return u;
}
public void Write(BinaryWriter writer)
{
writer.Write(genderName.ToString());
}
public void Read(BinaryReader reader)
{
genderName = new SqlString(reader.ReadString());
}
}
编辑
图片澄清: