Questions

General discussions related to using the C# Script Engine in APL+Win.

Moderators: Tech Support, phpbb_admin

Questions

Postby Ajay Askoolum » May 24th, 2014, 11:33 am

1. How do I reference the Entity Framework System.Data.Entity & System.Data.Entity.Spatial?

After:
⎕cse 'LoadAssembly' 'C:\Program Files\Microsoft SQL Server\110\Shared\Microsoft.SqlServer.Types.dll'
Name=Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=, PublicKey token=89-84-5D-CD-80-80-CC-91

2. Why does this fail?

⎕cse 'ExecStmt' 'SqlGeography thisPoint1 = new Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);'
¯1

Thanks for you insight.
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

How to use .Net SqlGeography in APL+Win

Postby joe_blaze » May 27th, 2014, 8:03 pm

Hi Ajay,

Thank you for asking this question.
I think that my answer will be instructive to others interested in using the APLNext C# Script Engine in APL+Win.
The problem you are having has nothing to do with APL+Win or the APLNext C# Script Engine,
APL+Win and the CSE can utilize properly designed C# executable statements and effectively use the .Net SqlGeography type.
Probably the most difficult part for an APL+Win programmer using C# is interpretation of the C# compiler errors.
Of course the reverse would be true for a C# programmer using APL+Win and trying to interpret the APL+Win exception messages (quad-DM).
Step-by-step, I will provide corrections to your C# executable statements, but here are few highlights:

    When the CSE 'ExecStmt' method returns -1, use the CSE 'GetLastError' to obtain the C# compiler error or alternately, set the CSE 'returnonerror' property to 1 to generate APL+Win exceptions for C# compiler errors
    One must either use the fully-qualified name of a .Net type, method or event or have previously executed an appropriate using statement
    There are often dependencies among .Net assemblies, e.g. Microsoft.SqlServer.Types requires a reference to System.Data
    Microsoft.SqlServer.Types.Point() is a method and not a constructor, so the 'new' prefix to Microsoft.SqlServer.Types.Point(...) is not correct

⎕cself←'C'⎕cse 'Init' 'System'
⍝↑ Successfully create an instance of the APLNext CSE called 'C' in the APL+Win session

⎕cse 'ExecStmt' 'using System;'
0
⍝↑ Successfully add a using statement to make all of the System namespace available without specifying the prefix 'System.', e.g. 'Int32' instead of '

⎕cse 'LoadAssembly' 'C:\Program Files\Microsoft SQL Server\110\Shared\Microsoft.SqlServer.Types.dll'
Name=Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=, PublicKey token=89-84-5D-CD-80-80-CC-91
⍝↑ Successfully load the Microsoft .Net assembly containing the .Net SqlGeography type

⎕cse 'ExecStmt' 'SqlGeography thisPoint1 = new Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);'
¯1
⍝↑ Fail in the attempt to create an instance of the SqlGeography type

⎕cse 'GetLastError'
(1,1): error CS0246: The type or namespace name 'SqlGeography' could not be found (are you missing a using directive or an assembly reference?)
⍝↑ Successfully obtain the C# compiler error reported

⎕cse 'ExecStmt' 'Microsoft.SqlServer.Types.SqlGeography thisPoint1 = new Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);'
¯1
⍝↑ Fail in the attempt to correct the prior statement by adding the fully-qualified name of the SqlGeography type to the left side of the equals sign in the C# executable statement.

⎕cse 'GetLastError'
(1,27): error CS0012: The type 'System.Data.SqlTypes.INullable' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
⍝↑ Successfully obtain the C# compiler error reported

⎕cse 'LoadAssemblyByName' 'System.Data'
Name=System.Data, Version=4.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89
⍝↑ Successfully load from the Global Assembly Cache (GAC) the Microsoft .Net System.Data assembly

⎕cse 'ExecStmt' 'Microsoft.SqlServer.Types.SqlGeography thisPoint1 = new Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);'
¯1
⍝↑ Fail in the attempt to create an instance of the SqlGeography type

⎕cse 'GetLastError'
(1,96): error CS0426: The type name 'Point' does not exist in the type 'Microsoft.SqlServer.Types.SqlGeography'
⍝↑ Successfully obtain the C# compiler error reported

⎕cse 'ExecStmt' 'Microsoft.SqlServer.Types.SqlGeography thisPoint1 = Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);'
0
⍝↑ Successfully create the instance of the SqlGeography type using SqlGeography.Point() which is a method not a constructor, so the 'new' prefix is not appropriate.
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Questions

Postby Ajay Askoolum » May 28th, 2014, 10:24 am

Thanks Joe.
The line I tried with CSE came straight from a VS2012 project. If the new keyword is unnecessary with CSE, so be it (not the end of the world!). Thus, it appears that you can work with the geography data type inside APL+Win. I'll resume my efforts in this direction.
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Questions

Postby joe_blaze » May 28th, 2014, 11:53 am

Hi Ajay,

Check out the intellisense or the Object Explorer in Visual Studio for the .Point() method to see that it is not a constructor, but a method.
Not all sample projects available on line work - did you try the sample project in Visual Studio?

Joe
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Questions

Postby Ajay Askoolum » June 1st, 2014, 2:47 pm

Thsnk you for your help Joe.
I have now been able to create 'thisPoint1' (using the latest version of CSE).

GEO
Name=Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=, PublicKey token=89-84-5D-CD-80-80-CC-91
Name=System.Data, Version=4.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89
0
⎕cse 'GetValue' 'thisPoint1'
46137132
⎕cse 'GetValue' 'thisPoint1.ToString()'
POINT (56.677 12.34)
⎕vr 'GEO'
∇ GEO
[1] ⎕cself←'c' ⎕cse 'Init' 'System'
[2] ⎕cse 'LoadAssembly' 'C:\Program Files\Microsoft SQL Server\110\Shared\Microsoft.SqlServer.Types.dll'
[3] ⎕cse 'LoadAssemblyByName' 'System.Data'
[4] ⎕cse 'ExecStmt' 'Microsoft.SqlServer.Types.SqlGeography thisPoint1 = Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);'
[5]


[NO, the coordinates have NOT been reversed - a really irritating 'feature' of Microsoft.]
You refer to sample projects: could you give me a specific URL? Thanks.

The geography functions in SQL Server 2008 R2 or later are very powerful; and CSE enables APL+Win to work with two further (already had DateTime) strongly data types namely geometry and geography.

In C#, I have two ways to tap into the geospatial functionality: the first is by reference to a library (as above) and the second is by using EntityFramework.6.1.0. How Do I do use the entity framework with CSE?

Given:

using System.Data;
using System.Data.SqlClient;
using System.Data.Entity; // Requires EntityFramework
using System.Data.Entity.Spatial;

using Microsoft.SqlServer;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes; // Requires reference to Microsoft.SQLServer.Types.dll

How do I use the first block of 'using'?

The C# lines of code (working in VS2012) are I am interested in are:
// POINT: Method 1 - DLL Reference
Microsoft.SqlServer.Types.SqlGeography thisPoint1 = Microsoft.SqlServer.Types.SqlGeography.Point(12.34, 56.677, 4326);

// POINT: Method2 - Entity Framework
System.Data.Entity.Spatial.DbGeography thisPoint = System.Data.Entity.Spatial.DbGeography.PointFromText(string.Format("POINT({0} {1})", -121.527200, 45.712113), 4326);
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Questions

Postby joe_blaze » June 1st, 2014, 9:36 pm

Hi Ajay,

I don't have a sample - I was asking you which sample you started with, because Point() is a method not a constructor.
I have used entity framework from Visual Studio, but not from the CSE.
In Visual Studio, the main benefit of entity framework is the database 'design surface'.
That won't be available in the CSE, because it exclusive to Visual Studio's GUI.
I will make some experiments to see what parts of entity framework would be accessible in the CSE.
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Questions

Postby Ajay Askoolum » June 2nd, 2014, 8:25 am

I did not use any sample project.

I started by researching for 3 things that interested me (all implemented in SQL Sever 2000 functions using trigonometric functions & which are very slow), namely, distance between two points, is a point inside a multi-vertex polygon, and do two polygons intersect.

The examples I found were of variable quality, relating to SQL Server 2008R2 & SQL Server 2012. However, taken together, the examples provided me with a reasonable start; thereafter, as I became familiar with the jargon, searches became much more productive.
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

CSE and Entity Framework

Postby joe_blaze » June 8th, 2014, 3:46 am

Hi Ajay,

My preliminary research into using the Entity Framework with the CSE is based on the four Microsoft videos available from http://msdn.microsoft.com/en-us/data/ee712907.
Some of the features of Entity Framework depend on NuGet and Visual Studio.
I will try to provide some examples using Entity Framework in the next version of the CSE documentation.
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361


Return to APL+Win & The C# Script Engine

Who is online

Users browsing this forum: No registered users and 17 guests