【问题标题】:C# and passing user defined types to SQL server over procedure parametersC# 并通过过程参数将用户定义的类型传递给 SQL Server
【发布时间】:2016-05-28 09:34:47
【问题描述】:

现在一直在寻找这个,但运气不佳。所以这就是我想要做的。

基本上,我有一些 GEOJson 结构形式的 C# 模型。所以我可以创建新的特征和特征集合等。当我序列化它时,它会吐出一些格式很好的 GEOJson。

现在我想将它保存到数据库中。为此,我想简单地将我的整个“模型”(FeatureCollection 或单个 Feature...)直接传递到存储过程中,该过程将处理将值推送到它们需要去的任何地方。是否可以在 SQL Server 中执行此操作?我对 SQL Server 有点陌生,并且大部分时间都在使用 Oracle,过去我使用 Oracle 的用户定义类型做过类似的事情。但不确定 SQL Server 是否可以做类似的事情。

这是我的 JSON 序列化后的样子:

{
    "type" : "FeatureCollection",
    "features" : [{
            "Type" : "Feature",
            "Properties" : {
                "name" : null,
                "description" : "Some Site Boundaries",
                "featureId" : 12,
                "featureTypeCode" : "siteboundary",
                "locationHierarchy" : {
                    "city" : {
                        "shortName" : "Long Beach",
                        "longName" : "Long Beach",
                        "hierarchyId" : 7,
                        "hierarchyParentId" : 4
                    },
                    "state" : {
                        "shortName" : "CA",
                        "longName" : "California",
                        "hierarchyId" : 4,
                        "hierarchyParentId" : 3
                    },
                    "country" : {
                        "shortName" : "U.S.A",
                        "longName" : "United States of America",
                        "hierarchyId" : 3,
                        "hierarchyParentId" : 2
                    }
                },
                "floors" : []
            },
            "Geometry" : {
                "type" : "Polygon",
                "coordinates" : [[[-118.1400864, 33.8324338], [-118.1401331, 33.8148000], [-118.1675695, 33.8148116], [-118.1677493, 33.8324253], [-118.1400864, 33.8324338]]]
            }
        }, {
            "Type" : "Feature",
            "Properties" : {
                "name" : null,
                "description" : "Some place",
                "featureId" : 71,
                "featureTypeCode" : "siteboundary",
                "locationHierarchy" : {
                    "city" : {
                        "shortName" : "St Louis",
                        "longName" : "St Louis",
                        "hierarchyId" : 8,
                        "hierarchyParentId" : 6
                    },
                    "state" : {
                        "shortName" : "MO",
                        "longName" : "Missouri",
                        "hierarchyId" : 6,
                        "hierarchyParentId" : 3
                    },
                    "country" : {
                        "shortName" : "U.S.A",
                        "longName" : "United States of America",
                        "hierarchyId" : 3,
                        "hierarchyParentId" : 2
                    }
                },
                "floors" : []
            },
            "Geometry" : {
                "type" : "Polygon",
                "coordinates" : [[[-90.3590381, 38.7637456], [-90.3390834, 38.7637456], [-90.3390834, 38.7474431], [-90.3590381, 38.7474431], [-90.3590381, 38.7637456]]]
            }
        }, {
            "Type" : "Feature",
            "Properties" : {
                "name" : null,
                "description" : null,
                "featureId" : 140,
                "featureTypeCode" : "siteboundary",
                "locationHierarchy" : {
                    "city" : {
                        "shortName" : "Mesa",
                        "longName" : "Mesa",
                        "hierarchyId" : 9,
                        "hierarchyParentId" : 5
                    },
                    "state" : {
                        "shortName" : "AZ",
                        "longName" : "Arizona",
                        "hierarchyId" : 5,
                        "hierarchyParentId" : 3
                    },
                    "country" : {
                        "shortName" : "U.S.A",
                        "longName" : "United States of America",
                        "hierarchyId" : 3,
                        "hierarchyParentId" : 2
                    }
                },
                "floors" : []
            },
            "Geometry" : {
                "type" : "Polygon",
                "coordinates" : [[[-111.7369067, 33.4783391], [-111.7368442, 33.4653505], [-111.7170835, 33.4653505], [-111.7170835, 33.4782348], [-111.7369067, 33.4783391]]]
            }
        }
    ]
}

然后,在应用程序层,我想以某种方式简单地将这个权利的整个模型传递给 SQL 服务器......例如:

public int CreateFeature(Feature json)
        {
            string result = string.Empty;
            int row;
            using (SqlConnection conn = new SqlConnection(_smmConnectionString))
            using (SqlCommand cmd = new SqlCommand("dbo.CreateFeature", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                // set up the parameters
                //cmd.Parameters.Add("@parm1in", SqlDbType.VarChar, 7);
                cmd.Parameters.Add("@Json", SqlDbType.Structured);

                // set parameter values
                cmd.Parameters["@Json"].Value = json;

                conn.Open();
                row = cmd.ExecuteNonQuery();
                conn.Close();
            }
            return row;
        }

然后我认为 SQL Server 中的过程可以简单地捕获该变量并根据需要对其进行解析:

SELECT @Json.feature[0].Type;

等等……

这可能吗?

【问题讨论】:

  • 您使用的是哪个版本的 SQL Server?从 2016 年开始,SQL Server 将提供原生 JSON 支持(在某种程度上是这样)。我相信有可能找到一些 CTP 并使用它直到发布日期。
  • 是的,我读到了... OPENJSON 我相信。虽然没有运行 2016,但该选项已失效。我认为与其浪费时间试图解决这个问题,我只是按照穷人的方式去做,并创建具有必要参数的程序,并根据需要使用每个程序。说实话,没有“那么多”..我只是想有一种巧妙的方式来做这件事并学习新的东西:)

标签: c# sql-server json geojson


【解决方案1】:

为什么不使用实体框架?

您可以通过在存储过程中创建整个数据存储逻辑来做到这一点,但我建议您不要这样做。编写存储过程和维护总是很痛苦的。使用存储过程的主要原因之一是需要真正密集的数据库使用和大量重复操作,因为它们缓存在数据库服务器中。不知道是不是你的情况?

你可以在这里阅读:http://www.seguetech.com/blog/06/04/Advantage-drawbacks-stored-procedures-processing-data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-24
    • 2010-11-02
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多