Solved

method in assembly is not static

Posted on 2006-11-04
10
1,517 Views
Last Modified: 2010-08-05
I am trying to use an assembly called from a function in SQL Server 2005,

Here is the assembly code:
Public Class Distance

    Public Function getDistance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double) As Double
        Dim theta As Double
        Dim dist As Double
        Dim distance As Double
        theta = lon1 - lon2
        dist = Math.Sin(CDbl(deg2rad(lat1))) * Math.Sin(CDbl(deg2rad(lat2))) + Math.Cos(CDbl(deg2rad(lat1))) * Math.Cos(CDbl(deg2rad(lat2))) * Math.Cos(CDbl(deg2rad(theta)))
        dist = Math.Acos(dist)
        dist = CDbl(rad2deg(dist))
        distance = dist * 60 * 1.1515
        Return distance
    End Function

    Private Function deg2rad(ByVal Deg As Double) As Double
        Return CDbl(Deg * Math.PI / 180)
    End Function

    Private Function rad2deg(ByVal Rad As Double) As Double
        Return CDbl(Rad * 180 / Math.PI)
    End Function

End Class

Here is the code to register the assembly with sql server:
CREATE ASSEMBLY asmDistanceFunctions
FROM 'E:\web\clients\out2daylive\Bin\asmDistanceFunctions.dll'
WITH PERMISSION_SET = SAFE
GO

Here is the code for the function calling the assembly - THIS IS WHERE I GET AN ERROR!
CREATE FUNCTION dbo.clrComputeDistance
(
  @lat1 as float,
  @lon1 as float,
  @lat2 as float,
  @lon2 as float
)
RETURNS float
AS EXTERNAL NAME [asmDistanceFunctions].[asmDistanceFunctions.Distance].[getDistance]
GO

I get the following error:
Msg 6573, Level 16, State 1, Procedure clrComputeDistance, Line 1
Method, property or field 'getDistance' of class 'asmDistanceFunctions.Distance' in assembly 'asmDistanceFunctions' is not static.

How do I correct this?
0
Comment
Question by:alivemedia
  • 5
  • 4
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17873718
Public Class Distance

    Public Static Function getDistance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double) As Double
        Dim theta As Double
        Dim dist As Double
        Dim distance As Double
        theta = lon1 - lon2
        dist = Math.Sin(CDbl(deg2rad(lat1))) * Math.Sin(CDbl(deg2rad(lat2))) + Math.Cos(CDbl(deg2rad(lat1))) * Math.Cos(CDbl(deg2rad(lat2))) * Math.Cos(CDbl(deg2rad(theta)))
        dist = Math.Acos(dist)
        dist = CDbl(rad2deg(dist))
        distance = dist * 60 * 1.1515
        Return distance
    End Function

    Private Function deg2rad(ByVal Deg As Double) As Double
        Return CDbl(Deg * Math.PI / 180)
    End Function

    Private Function rad2deg(ByVal Rad As Double) As Double
        Return CDbl(Rad * 180 / Math.PI)
    End Function

End Class
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17873876
Of course, you could always also do this as a regular T-SQL function as well and not have the overhead of having to load an assembly (returns distance in meters):

CREATE FUNCTION DistanceBetween2PointsQ( @Lat1 float,@Long1 float, @Lat2 float, @Long2 float)
returns float as
return
    ((Abs(60 * (ACos((Sin(@Lat1 * (PI()/180)) * Sin(@Lat2 * (PI()/180)))
     + (Cos(@Lat1 * (PI()/180)) * Cos(@Lat2 * (PI()/180)) * Cos((@Long2 * (PI()/180) * -1)
     - (@Long1 * (PI()/180) * -1)))) * (180/PI())))) * 1852)

GO
declare @Lat1 float
declare @Long1 float
declare @Lat2 float
declare @Long2 float
Set @Lat1 = 48.06361
Set @Long1 = 8.4251385

Set @Lat2 = 48.063526
Set @Long2 = 8.4250002

SELECT dbo.DistanceBetween2Points(@Lat1,@Long1,@Lat2,@Long2)
0
 
LVL 2

Author Comment

by:alivemedia
ID: 17874329
actually doing it the way I am trying to will be several times faster.  right now it takes more than a second to run my query - that's just 1 user with no one else hitting the database.  From what I have read this should reallypeed this up for me since I am using SQl Server 2005 has asp.net CLR hosted in it.
0
 
LVL 2

Author Comment

by:alivemedia
ID: 17874334
AngelII that gives me the following error:  Methods cannot be declared static

I am sure I am missing something small here
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17874389
for visual basic you need to use Shared
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:alivemedia
ID: 17874401
I figured it out, I needed to add shared to all of my functions:

Public Class Distance

    Public Shared Function getDistance(ByVal lat1 As Double, ByVal lon1 As Double, ByVal lat2 As Double, ByVal lon2 As Double) As Double
        Dim theta As Double
        Dim dist As Double
        Dim distance As Double
        theta = lon1 - lon2
        dist = Math.Sin(CDbl(deg2rad(lat1))) * Math.Sin(CDbl(deg2rad(lat2))) + Math.Cos(CDbl(deg2rad(lat1))) * Math.Cos(CDbl(deg2rad(lat2))) * Math.Cos(CDbl(deg2rad(theta)))
        dist = Math.Acos(dist)
        dist = CDbl(rad2deg(dist))
        distance = dist * 60 * 1.1515
        Return distance
    End Function

    Private Shared Function deg2rad(ByVal Deg As Double) As Double
        Return CDbl(Deg * Math.PI / 180)
    End Function

    Private Shared Function rad2deg(ByVal Rad As Double) As Double
        Return CDbl(Rad * 180 / Math.PI)
    End Function

End Class

0
 
LVL 29

Expert Comment

by:Nightman
ID: 17874407
that's what I said.

Also make sure that you have enabled clr

EXEC sp_configure 'clr enabled',1
RECONFIGURE WITH OVERRIDE
GO
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17874476
benchmarks on selecting variable long lat and a calculating distance from a defined position for 100,000 rows from a spatial database (OK, I'm doing a clustered index scan, with top 100,000 so this is pretty straightforward and provides a like for like comparison).

with CLR:
436 ms

with T-SQL UDF
548 ms

I eliminated the FIRST execution of the CLR process, as this had a small overhead in loading into memory for the first time (negligible, really) but wanted to make it accurate.

Then upped to 1,000,000 rows. CLR=8.2 seconds, T-SQL = 11.3 seconds.

In the long run the CLR does appear to be faster. But only significant at high volumes.
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 17874480
CLR comes with the extra overhead of requiring the .NET framework and SQL 2005 - T-SQL will work fine on SQL 2000 (depending on your requirements this may be fine for you). However, there are documented instances of SQL not freeing up the resources when you want to deploy a new version of the assembly - you DON'T want to have to restart a production SQL Server simply to deploy.

Also, when mirroring (or log shipping or using peer-to-peer replication) the embedded assemblies are not mirrored - only the data and schema. This places an administrative overhead (documentation on change control) - again, this may not be a concern for you - just want to make sure you are aware of it.

Cheers
Night
0
 
LVL 2

Author Comment

by:alivemedia
ID: 17874797
thanks for the great info - definitly something to keep in mind.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now