try 从System.Data.SQLClient迁移出go ,在处理用户定义类型时,我们遇到了使用SQLGeography对象的操作障碍. 简而言之,我可以在一个简单的SQLDataRecord构造中设置和检索序号值,但是检索值返回以下异常:
Value cannot be null. (Parameter 'key')
堆栈跟踪:
at System.ThrowHelper.ThrowArgumentNullException(String name)
at System.Collections.Concurrent.ConcurrentDictionary\`2.TryGetValue(TKey key, TValue& value)
at Microsoft.Data.SqlClient.Server.SerializationHelperSql9.GetSerializer(Type t)
at Microsoft.Data.SqlClient.Server.SerializationHelperSql9.Deserialize(Stream s, Type resultType)
at Microsoft.Data.SqlClient.Server.ValueUtilsSmi.GetUdt_LengthChecked(SmiEventSink_Default sink, ITypedGettersV3 getters, Int32 ordinal, SmiMetaData metaData)
at Microsoft.Data.SqlClient.Server.ValueUtilsSmi.GetValue(SmiEventSink_Default sink, ITypedGettersV3 getters, Int32 ordinal, SmiMetaData metaData, Object context)
at Microsoft.Data.SqlClient.Server.ValueUtilsSmi.GetValue200(SmiEventSink_Default sink, SmiTypedGetterSetter getters, Int32 ordinal, SmiMetaData metaData, Object context)
at Microsoft.Data.SqlClient.Server.SqlDataRecord.GetValueFrameworkSpecific(Int32 ordinal)
at Microsoft.Data.SqlClient.Server.SqlDataRecord.GetValue(Int32 ordinal)
at SqlGeographyTest.Program.Main(String\[\] args) in C:\\source\\repos\\SqlServerTestError\\SqlServerTestError\\Program.cs:line 27
如果你知道我在哪里出错就太好了. 谢谢
再现问题的代码示例:
using Microsoft.Data.SqlClient.Server;
using Microsoft.SqlServer.Types;
using System.Data;
namespace SqlGeographyTest
{
class Program
{
static void Main(string[] args)
{
SqlGeography geog = CreateGeographyPoint(40, 90);
var md1 = new SqlMetaData("ID", SqlDbType.Int);
var md2 = new SqlMetaData("geog", SqlDbType.Udt, typeof(SqlGeography), "geography");
SqlMetaData[] metadata = [md1, md2];
SqlDataRecord record = new SqlDataRecord(metadata);
record.SetValues(1, geog);
var checkValue = record.GetValue(0);
var checkValue2 = record.GetOrdinal("geog");
try
{
var checkValue3 = record.GetValue(1);
}
catch(Exception ex)
{
string errorMessage = ex.Message;
}
}
public static SqlGeography CreateGeographyPoint(double longitude, double latitude)
{
var text = string.Format("POINT({0} {1})", longitude, latitude);
var ch = new System.Data.SqlTypes.SqlChars(text);
return Microsoft.SqlServer.Types.SqlGeography.STPointFromText(ch, 4326);
}
}
}
我希望返回的是SQLGeography对象,而不是抛出异常.