简体   繁体   English

传递DBGeometry列表以检索SQL中包含的点

[英]Passing a List of DBGeometry to retrieve points contained from SQL

I have a dataset on my SQL server that contains a few million Geography Points that I want to retrieve based on a grid of DBGeography rectangles I create on the client side. 我在SQL Server上有一个数据集,其中包含要基于客户端创建的DBGeography矩形网格检索的数百万个Geography Point。 Basically I want to pass in ac# List of DBGeography , and my server would hand back the list of points that fit into those geometries, along with the DBGeography they fall into. 基本上,我想传递ac#DBGeography列表,我的服务器将把适合这些几何的点的列表以及它们所属的DBGeography递回。 I am currently querying for each individual rectangle, causing a lot of queries if my map I'm pulling is divided into a large number of tiles. 我目前正在查询每个单独的矩形,如果我要拉的地图被分成大量的图块,则会引起很多查询。 Obviously the performance suffers. 显然性能受到影响。

I am currently using the code below. 我目前正在使用下面的代码。

var pointsInRectangle = (from p in MyTable.Points
                                       where MyRectangle.Intersects(p.Location)
                                       select p).ToList();

Question: How would I alter it to pass in a List of DBGeography Rectangles and get the corresponding data back? 问题:如何更改它以传递DBGeography矩形列表并获取相应的数据?

MyRectangle is a DBGeography I create in C# and Location is the point geography in my SQL table. MyRectangle是我在C#中创建的DBGeography,而Location是SQL表中的点地理。 This code currently works but only for one cell at a time. 该代码当前有效,但一次仅适用于一个单元格。 I tried a few things but to no avail. 我尝试了几件事,但无济于事。 Entity Framework is not my specialty, but I also wouldn't know how in straight SQL (though I'm fine using SQL if needed for a solution). 实体框架不是我的专长,但是我也不知道如何使用直接SQL(尽管如果需要解决方案,我可以使用SQL)。

Also I tried pulling all the points (from the larger outer rectangle) to the client, and then processing on the client side to subdivide it into a grid. 我还尝试将所有点(从较大的外部矩形中)拉到客户端,然后在客户端进行处理以将其细分为网格。 The performance is very slow even though the initial query is almost instant (probably because of indexes). 即使初始查询几乎是即时的(可能是由于索引),性能仍然很慢。

You would have to do a couple things. 您将不得不做几件事。 Create a table type in SQL (code below), create a Stored Procedure to accept the Table Type and select from that table type (select example below), and then in your C# code populate the SQL type and pass it to a SQLCommand calling the Stored Procedure you created. 在SQL中创建一个表类型(下面的代码),创建一个存储过程以接受表类型并从该表类型中选择(在下面的示例中选择),然后在您的C#代码中填充SQL类型并将其传递给调用您创建的存储过程。 (populating the Table Type variable is below, not the rest of the SQLCommand. (下面是填充Table Type变量,而不是其余的SQLCommand。

// create table type in SQL (this is an example in case you need to pass more then 1 field/value
CREATE TYPE [dbo].[NameValuePairTable] AS TABLE(
    [Name] [NVARCHAR](MAX) NULL,
    [Value] [NVARCHAR](MAX) NULL
)
GO

create SP here with namevalue pair paramater 使用namevalue对参数在此处创建SP

Create PROCEDURE [dbo].[SPPassingTableType]         
    @NameValuePairAnswers as dbo.NameValuePairTable READONLY    
AS
BEGIN
    -- do your SQL logic stuff here

    -- this will select from the table type you passed to the SP
    SELECT * 
    FROM @NameValuePairAnswers  
END

in your c# code you could do this. 在您的C#代码中,您可以执行此操作。 NOTE: This is not FULL C# code, this is just showing how to handle the table type variable and populate it. 注意:这不是完整的C#代码,仅显示如何处理表类型变量并填充它。 You still have to generate your SQL Command and pass this table type to the SQL Command 您仍然必须生成SQL命令并将此表类型传递给SQL命令

// now build out the namevalue pair paramater
                SqlParameter TableData = new SqlParameter();
                TableData.ParameterName = "@NameValuePairAnswers";
                TableData.TypeName = "dbo.NameValuePairTable";
                TableData.SqlDbType = SqlDbType.Structured;
                TableData.Value = dtFormResultsToSave; 

Where the dtFormResultsToSave is a datatable that matches your Table Type in SQL above. 其中dtFormResultsToSave是与上述SQL中的表类型匹配的数据表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM