ho creato una funzione scalare nel DBCome utilizzare le funzioni definite dall'utente SQL in .NET?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_GetUserId_Username]
(
@Username varchar(32)
)
RETURNS int
AS
BEGIN
DECLARE @UserId int
SELECT @UserId = UserId FROM [User] WHERE Username = @Username
RETURN @UserId
END
Ora voglio farlo funzionare nel mio NET C# o VB.NET codice.
Uso Entity Framework, ho provato a mapparlo con la funzione mapping e non ho avuto successo. non mi importa di farlo con semplice DbCommand, il problema è che non ottengo risultati (la funzione esiste nella classe Entità):
public int GetUserIdByUsername(string username)
{
EntityConnection connection = (EntityConnection)Connection;
DbCommand com = connection.StoreConnection.CreateCommand();
com.CommandText = "fn_GetUserId_Username";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("Username", username));
if (com.Connection.State == ConnectionState.Closed) com.Connection.Open();
try
{
var result = com.ExecuteScalar(); //always null
}
catch (Exception e)
{
}
return result;
}
C'è qualche soluzione? I post in C# o VB.NET saranno accolti.
Risultato: SqlException: 'fn_GetUserId_Username' non è un nome di funzione riconosciuta. – Shimmy
@Shimmy: specifica il nome del proprietario, "dbo" -> "seleziona dbo.fn_GetUserId_Username (@Username)" – Sung
Funzionante, grazie! – Shimmy