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对象,而不是抛出异常.

推荐答案

不知道为什么它不工作,可能值得向Microsoft报告,但您可以try 手动删除SqlBytes:

var sqlBytes = record.GetSqlBytes(1);
var deserialize = SqlGeography.Deserialize(sqlBytes);

Csharp相关问答推荐

System.Data.SQLite:判断SQLite数据库是否为空(任何表中至少有一行)

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

ASP.NET核心结果.文件vs结果.流

通过EFCore上传大量数据.

为什么C#Bigbit不总是相同的比特长度?

. NET JSON属性自定义所需逻辑

模型绑定RazorPage表单

在.NET核心项目中创建Startup.cs比在Program.cs中注册服务好吗?

如何在VS代码中为C#DotNet配置.json选项以调试内部终端的控制台应用程序

如何在Cosmos SDK中控制超时、重试和重试之间的延迟?

EF核心区分大小写的主键

如何使用用于VS代码的.NET Maui扩展在我的iOS/Android设备或模拟器上进行调试?

如何在.NET AOT中为所有枚举启用JsonStringEnumConverter

泛型参数在.NET 8 AOT中没有匹配的批注

如何在microsoft.clearscript.v8的jsondata中使用Linq

正在try 将自定义字体添加到我的控制台应用程序

如何保存具有多个重叠图片框的图片框?

为什么Visual Studio 2022建议IDE0251将我的方法设置为只读?

使用生产环境调试我的应用程序的快速方法

不寻常的C#语法