Showing posts with label assembly. Show all posts
Showing posts with label assembly. Show all posts

Wednesday, March 7, 2012

Problem Creating Stored Procedure From A Library, DLL

I'm having a problem creating Stored Procedure from a library I have created. I have found that the Create Assembly query causes the Assemby to be placed into the Master Database's Assembly collection. The other is that the Create Procedure Query states that it cannot find the type in the assembly. Can someone see what is going on and let me know

I have also changed the "struct" to a "class" and I get the same error message.

I've included the code snippit, and queries belows:

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace HHLibrary
{
[Serializable]
public struct State
{
[SqlFunction(Name = "GetStateIndexByName",
IsDeterministic = true,
DataAccess = DataAccessKind.Read)]
public static SqlInt32 GetStateIndexByName(SqlString Name, out SqlByte StateIndex)
{
SqlInt32 iResult = 0;
StringBuilder sb = new StringBuilder(500);
SqlCommand command = new SqlCommand();

StateIndex = 255; // Set the output parameter to an illegal value

. . .

return iResult;
}

}
}

************************ Compilation Messages ******************************************

Build started: Project: HeadHunterLibrary, Configuration: Debug Any CPU
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /optimize- /out:obj\Debug\HeadHunterLibrary.dll /target:library State.cs Properties\AssemblyInfo.cs

Compile complete -- 0 errors, 0 warnings
HeadHunterLibrary -> C:\HHLibraries\HeadHunterLibrary.dll
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

************************ Trans-SQL Query ******************************************

create assembly HeadHunterLibrary
FROM 'C:\HHLibraries\HeadHunterLibrary.dll'
WITH PERMISSION_SET = SAFE;
GO

CREATE PROCEDURE dbo.GetStateIndexByName
( @.Name nvarchar(25),
@.StateIndex tinyint
)
AS EXTERNAL NAME HeadHunterLibrary.State.GetStateIndexByName;
GO


************************ Messages From Executing The Query ************************************

Msg 6505, Level 16, State 1, Procedure GetStateIndexByName, Line 2
Could not find Type 'State' in assembly 'HeadHunterLibrary'.

You need to specify the database in use. CREATE ASSEMBLY add assemblies to the database in use. For example:

use MyDatabase
create assembly HeadHunterLibrary
FROM 'C:\HHLibraries\HeadHunterLibrary.dll'
WITH PERMISSION_SET = SAFE;
GO

You can double check that it worked by:

SELECT * from myDatabase.sys.assemblies

You need to square bracket the namespace.class. As was written thinks HeadHunterLibrary is a database name. For example:

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

Dan


|||

Made the corrections you recommended. Still have one problem that I don’t understand. The query I execute to create the stored procedure is:

USE HeadHunter

CREATE PROCEDURE dbo.GetStateIndexByName

( @.Name nvarchar(25),

@.StateIndex tinyint OUTPUT

)

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

When I execute the Create Procedure dbo.GetStateIndexByName I get the following error message.

Msg 102, Level 15, State 1, Procedure GetStateIndexByName, Line 5 Incorrect syntax near ';'.

The managed code looks like this:

namespace HeadHunterLibrary

{

[Serializable]

public class State

{

public static SqlInt32 GetStateIndexByName(SqlString Name, out SqlByte StateIndex)

{

. . .

}

}

}

Can you see what is causing the error?

Thank You

Dan Marks

Logos-Systems@.mchsi.com

|||

Made the corrections you recommended. Still have one problem that I don’t understand. The query I execute to create the stored procedure is:

USE HeadHunter

CREATE PROCEDURE dbo.GetStateIndexByName

( @.Name nvarchar(25),

@.StateIndex tinyint OUTPUT

)

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

When I execute the Create Procedure dbo.GetStateIndexByName I get the following error message.

Msg 102, Level 15, State 1, Procedure GetStateIndexByName, Line 5 Incorrect syntax near ';'.

The managed code looks like this:

namespace HeadHunterLibrary

{

[Serializable]

public class State

{

public static SqlInt32 GetStateIndexByName(SqlString Name, out SqlByte StateIndex)

{

. . .

}

}

}

Can you see what is causing the error?

Thank You

Dan Marks

Logos-Systems@.mchsi.com

|||

Made the corrections you recommended. Still have one problem that I don’t understand. The query I execute to create the stored procedure is:

USE HeadHunter

CREATE PROCEDURE dbo.GetStateIndexByName

( @.Name nvarchar(25),

@.StateIndex tinyint OUTPUT

)

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

When I execute the Create Procedure dbo.GetStateIndexByName I get the following error message.

Msg 102, Level 15, State 1, Procedure GetStateIndexByName, Line 5 Incorrect syntax near ';'.

The managed code looks like this:

namespace HeadHunterLibrary

{

[Serializable]

public class State

{

public static SqlInt32 GetStateIndexByName(SqlString Name, ref SqlByte StateIndex)

{

. . .

}

}

}

Can you see what is causing the error?

Thank You

Dan Marks

Logos-Systems@.mchsi.com

|||

I see several problems.

The error is probably caused by the lack of assembly name in:

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

Try this:

AS EXTERNAL NAME [AssemblyName].[HeadHunterLibrary.State].GetStateIndexByName;

You are also missing a return value (your procedure returns a SqlInt32, but you don't specify that in the stored procedure definition).

Finally, you don't have a StoredProcedure attribute on your method, put this on the line before the method:

[Microsoft.SqlServer.Server.SqlProcedure]

Hope this helps.

|||

Namespace problem?

CREATE PROCEDURE hello

AS

EXTERNAL NAME HelloWorld.StoredProcedures.HelloWorld

GO

EXEC hello

Msg 6505, Level 16, State 1, Procedure hello, Line 1

Could not find Type 'StoredProcedures' in assembly 'HelloWorld'.

Msg 2812, Level 16, State 62, Line 1

Could not find stored procedure 'hello'.

CREATE PROCEDURE hello

AS

EXTERNAL NAME HelloWorld.[HelloWorld.StoredProcedures].HelloWorld

GO

EXEC hello

Hello world! It's now 18/04/2007 14:26:23

try something like:

CREATE PROCEDURE dbo.GetStateIndexByName
( @.Name nvarchar(25),
@.StateIndex tinyint
)
AS EXTERNAL NAME HeadHunterLibrary.[HHLibrary.State].GetStateIndexByName;
GO

Problem Creating Stored Procedure From A Library, DLL

I'm having a problem creating Stored Procedure from a library I have created. I have found that the Create Assembly query causes the Assemby to be placed into the Master Database's Assembly collection. The other is that the Create Procedure Query states that it cannot find the type in the assembly. Can someone see what is going on and let me know

I have also changed the "struct" to a "class" and I get the same error message.

I've included the code snippit, and queries belows:

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace HHLibrary
{
[Serializable]
public struct State
{
[SqlFunction(Name = "GetStateIndexByName",
IsDeterministic = true,
DataAccess = DataAccessKind.Read)]
public static SqlInt32 GetStateIndexByName(SqlString Name, out SqlByte StateIndex)
{
SqlInt32 iResult = 0;
StringBuilder sb = new StringBuilder(500);
SqlCommand command = new SqlCommand();

StateIndex = 255; // Set the output parameter to an illegal value

. . .

return iResult;
}

}
}

************************ Compilation Messages ******************************************

Build started: Project: HeadHunterLibrary, Configuration: Debug Any CPU
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /optimize- /out:obj\Debug\HeadHunterLibrary.dll /target:library State.cs Properties\AssemblyInfo.cs

Compile complete -- 0 errors, 0 warnings
HeadHunterLibrary -> C:\HHLibraries\HeadHunterLibrary.dll
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

************************ Trans-SQL Query ******************************************

create assembly HeadHunterLibrary
FROM 'C:\HHLibraries\HeadHunterLibrary.dll'
WITH PERMISSION_SET = SAFE;
GO

CREATE PROCEDURE dbo.GetStateIndexByName
( @.Name nvarchar(25),
@.StateIndex tinyint
)
AS EXTERNAL NAME HeadHunterLibrary.State.GetStateIndexByName;
GO


************************ Messages From Executing The Query ************************************

Msg 6505, Level 16, State 1, Procedure GetStateIndexByName, Line 2
Could not find Type 'State' in assembly 'HeadHunterLibrary'.

You need to specify the database in use. CREATE ASSEMBLY add assemblies to the database in use. For example:

use MyDatabase
create assembly HeadHunterLibrary
FROM 'C:\HHLibraries\HeadHunterLibrary.dll'
WITH PERMISSION_SET = SAFE;
GO

You can double check that it worked by:

SELECT * from myDatabase.sys.assemblies

You need to square bracket the namespace.class. As was written thinks HeadHunterLibrary is a database name. For example:

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

Dan


|||

Made the corrections you recommended. Still have one problem that I don’t understand. The query I execute to create the stored procedure is:

USE HeadHunter

CREATE PROCEDURE dbo.GetStateIndexByName

( @.Name nvarchar(25),

@.StateIndex tinyint OUTPUT

)

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

When I execute the Create Procedure dbo.GetStateIndexByName I get the following error message.

Msg 102, Level 15, State 1, Procedure GetStateIndexByName, Line 5 Incorrect syntax near ';'.

The managed code looks like this:

namespace HeadHunterLibrary

{

[Serializable]

public class State

{

public static SqlInt32 GetStateIndexByName(SqlString Name, out SqlByte StateIndex)

{

. . .

}

}

}

Can you see what is causing the error?

Thank You

Dan Marks

Logos-Systems@.mchsi.com

|||

Made the corrections you recommended. Still have one problem that I don’t understand. The query I execute to create the stored procedure is:

USE HeadHunter

CREATE PROCEDURE dbo.GetStateIndexByName

( @.Name nvarchar(25),

@.StateIndex tinyint OUTPUT

)

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

When I execute the Create Procedure dbo.GetStateIndexByName I get the following error message.

Msg 102, Level 15, State 1, Procedure GetStateIndexByName, Line 5 Incorrect syntax near ';'.

The managed code looks like this:

namespace HeadHunterLibrary

{

[Serializable]

public class State

{

public static SqlInt32 GetStateIndexByName(SqlString Name, out SqlByte StateIndex)

{

. . .

}

}

}

Can you see what is causing the error?

Thank You

Dan Marks

Logos-Systems@.mchsi.com

|||

Made the corrections you recommended. Still have one problem that I don’t understand. The query I execute to create the stored procedure is:

USE HeadHunter

CREATE PROCEDURE dbo.GetStateIndexByName

( @.Name nvarchar(25),

@.StateIndex tinyint OUTPUT

)

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

When I execute the Create Procedure dbo.GetStateIndexByName I get the following error message.

Msg 102, Level 15, State 1, Procedure GetStateIndexByName, Line 5 Incorrect syntax near ';'.

The managed code looks like this:

namespace HeadHunterLibrary

{

[Serializable]

public class State

{

public static SqlInt32 GetStateIndexByName(SqlString Name, ref SqlByte StateIndex)

{

. . .

}

}

}

Can you see what is causing the error?

Thank You

Dan Marks

Logos-Systems@.mchsi.com

|||

I see several problems.

The error is probably caused by the lack of assembly name in:

AS EXTERNAL NAME [HeadHunterLibrary.State].GetStateIndexByName;

Try this:

AS EXTERNAL NAME [AssemblyName].[HeadHunterLibrary.State].GetStateIndexByName;

You are also missing a return value (your procedure returns a SqlInt32, but you don't specify that in the stored procedure definition).

Finally, you don't have a StoredProcedure attribute on your method, put this on the line before the method:

[Microsoft.SqlServer.Server.SqlProcedure]

Hope this helps.

|||

Namespace problem?

CREATE PROCEDURE hello

AS

EXTERNAL NAME HelloWorld.StoredProcedures.HelloWorld

GO

EXEC hello

Msg 6505, Level 16, State 1, Procedure hello, Line 1

Could not find Type 'StoredProcedures' in assembly 'HelloWorld'.

Msg 2812, Level 16, State 62, Line 1

Could not find stored procedure 'hello'.

CREATE PROCEDURE hello

AS

EXTERNAL NAME HelloWorld.[HelloWorld.StoredProcedures].HelloWorld

GO

EXEC hello

Hello world! It's now 18/04/2007 14:26:23

try something like:

CREATE PROCEDURE dbo.GetStateIndexByName
( @.Name nvarchar(25),
@.StateIndex tinyint
)
AS EXTERNAL NAME HeadHunterLibrary.[HHLibrary.State].GetStateIndexByName;
GO

Saturday, February 25, 2012

problem creating assembly using other assemblies

Hi,

I have developed a Stored procedure to output text to a text file, after doing a lot of reading here.
I want to be able to output data to a Postgres database, to be used for web mapping. I have referenced an Assembly Npgsql.dll so I can send updates to the postgres database when my ms sql database is updated. However, after building my class library, when I try to CREATE ASSEMBLY in sql server 2005 express, it sends out a message:
Msg 10301, Level 16, State 1, Line 1
Assembly 'ClassLibrary1' references assembly 'system.drawing, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: 2(error not found)). Please load the referenced assembly into the current database and retry your request.
The error seems to be in the Npgsql.dll assembly, can anybody please shed some light on this.
I tried to load the system.drawing assembly, and can do so in Unrestricted mode. It then spits out the same problem for System.Windows.Forms.dll.
I am still very new to all of this so any help would be appreciated.
Cheers,
Jatz91.

I got around this problem.

I used CREATE ASSEMBLY with PERMISSION_SET=UNSAFE and put the Microsoft.NET Framework Assemblies in the same folder where my built assembly was.

When I used CREATE ASSEMBLY, a warning was spat out for each assembly saying:

Warning: The Microsoft .Net frameworks assembly 'system.drawing, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a, processorarchitecture=msil.' you are registering is not fully tested in SQL Server hosted environment.

So now I run my CLR in UNSAFE mode

Cheers

Problem creating a function from an assembly

I have compiled a dll with the following code:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlTypes

Public Class SICTrans

Public Shared Function TransSIC(ByVal inpSIC As String) As String
'Dim conn As SqlConnection = New SqlConnection("context connection=true")
Dim NewSIC, TempSIC, tempFSIC As String

If Len(NZ(inpSIC)) > 0 Then
TempSIC = NZ(inpSIC)

If Len(TempSIC) < 5 Then TempSIC = Left("00000", 5 - Len(TempSIC))

tempFSIC = Left(TempSIC, 2) + "." + Mid(TempSIC, 3, 2)

If Val(Right(TempSIC, 1)) > 0 Then

tempFSIC = tempFSIC + "/" + Right(TempSIC, 1)
End If
End If

NewSIC = tempFSIC

TransSIC = NewSIC

End Function
Public Shared Function NZ(ByVal input As String) As String
If Not (input Is Nothing) Then
Return input
Exit Function
End If
Return String.Empty

End Function
End Class

Which compiles fine...

i then use the following code to create the assembly in SQL which is fine:

USE NARD
GO
CREATE ASSEMBLY SICCodeTrans
FROM 'c:\SICCodeTrans.dll'
WITH PERMISSION_SET = SAFE
GO

but when i goto create the function with the following code it wont have it!

CREATE FUNCTION TransSICCode(@.inpSIC varchar)
RETURNS varchar
AS EXTERNAL NAME
SICCodeTrans.SICTrans.TransSIC
GO

It gives me the following error message

Msg 6505, Level 16, State 1, Procedure TransSICCode, Line 1
Could not find Type 'SICTrans' in assembly 'SICCodeTrans'.

Any ideas?

Thanks

Marek Kluczynski

I think what's going on here is that there is a root namespace on the project.

IIRC, in a C# project VS will simply insert the root namespace delcaration into your code when you use certain templates. In VB, on the other hand, the compiler decides to insert the namespace definition without having it show up in the code.

When you go to create the function in SQL, it can't find it because there is no SICTrans class in the default namespace, there is a SICTrans class in whatever root namespace was set on the project.

You can right-click on the project in VS and pull up the propertiesunder the "Application" tab you'll see this.

Assuming your namespace is NAMESPACE, you'd think you could do the following:

CREATE FUNCTION TransSICCode(@.inpSIC varchar)
RETURNS varchar
AS EXTERNAL NAME
SICCodeTrans.NAMESPACE.SICTrans.TransSIC
GO

This won't workit's a (rather annoying) syntax error. You have to write it like this:

CREATE FUNCTION TransSICCode(@.inpSIC varchar)
RETURNS varchar
AS EXTERNAL NAME
SICCodeTrans.[NAMESPACE.SICTrans].TransSIC
GO

Hope this works for you. Let us know. :)

Cheers
-Isaac

|||

yes this seems to have resolved the namespace error.

I now have the following error:

Msg 6551, Level 16, State 2, Procedure TransSICCode, Line 1

CREATE FUNCTION for "TransSICCode" failed because T-SQL and CLR types for return value do not match.

Many thanks

Marek

|||I suspect two things. First, you should map the string to an nvarchar, not a varcharCLR strings are Unicode. Second, you need to give a parameter to the nvarchar, e.g., nvarchar(4000), nvarchar(max), etc.

Cheers,
-Isaac|||Many thanks for all your help

Problem creating a function from an assembly

I have compiled a dll with the following code:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlTypes

Public Class SICTrans

Public Shared Function TransSIC(ByVal inpSIC As String) As String
'Dim conn As SqlConnection = New SqlConnection("context connection=true")
Dim NewSIC, TempSIC, tempFSIC As String

If Len(NZ(inpSIC)) > 0 Then
TempSIC = NZ(inpSIC)

If Len(TempSIC) < 5 Then TempSIC = Left("00000", 5 - Len(TempSIC))

tempFSIC = Left(TempSIC, 2) + "." + Mid(TempSIC, 3, 2)

If Val(Right(TempSIC, 1)) > 0 Then

tempFSIC = tempFSIC + "/" + Right(TempSIC, 1)
End If
End If

NewSIC = tempFSIC

TransSIC = NewSIC

End Function
Public Shared Function NZ(ByVal input As String) As String
If Not (input Is Nothing) Then
Return input
Exit Function
End If
Return String.Empty

End Function
End Class

Which compiles fine...

i then use the following code to create the assembly in SQL which is fine:

USE NARD
GO
CREATE ASSEMBLY SICCodeTrans
FROM 'c:\SICCodeTrans.dll'
WITH PERMISSION_SET = SAFE
GO

but when i goto create the function with the following code it wont have it!

CREATE FUNCTION TransSICCode(@.inpSIC varchar)
RETURNS varchar
AS EXTERNAL NAME
SICCodeTrans.SICTrans.TransSIC
GO

It gives me the following error message

Msg 6505, Level 16, State 1, Procedure TransSICCode, Line 1
Could not find Type 'SICTrans' in assembly 'SICCodeTrans'.

Any ideas?

Thanks

Marek Kluczynski

I think what's going on here is that there is a root namespace on the project.

IIRC, in a C# project VS will simply insert the root namespace delcaration into your code when you use certain templates. In VB, on the other hand, the compiler decides to insert the namespace definition without having it show up in the code.

When you go to create the function in SQL, it can't find it because there is no SICTrans class in the default namespace, there is a SICTrans class in whatever root namespace was set on the project.

You can right-click on the project in VS and pull up the propertiesunder the "Application" tab you'll see this.

Assuming your namespace is NAMESPACE, you'd think you could do the following:

CREATE FUNCTION TransSICCode(@.inpSIC varchar)
RETURNS varchar
AS EXTERNAL NAME
SICCodeTrans.NAMESPACE.SICTrans.TransSIC
GO

This won't workit's a (rather annoying) syntax error. You have to write it like this:

CREATE FUNCTION TransSICCode(@.inpSIC varchar)
RETURNS varchar
AS EXTERNAL NAME
SICCodeTrans.[NAMESPACE.SICTrans].TransSIC
GO

Hope this works for you. Let us know. :)

Cheers
-Isaac

|||

yes this seems to have resolved the namespace error.

I now have the following error:

Msg 6551, Level 16, State 2, Procedure TransSICCode, Line 1

CREATE FUNCTION for "TransSICCode" failed because T-SQL and CLR types for return value do not match.

Many thanks

Marek

|||I suspect two things. First, you should map the string to an nvarchar, not a varcharCLR strings are Unicode. Second, you need to give a parameter to the nvarchar, e.g., nvarchar(4000), nvarchar(max), etc.

Cheers,
-Isaac|||Many thanks for all your help