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
現在,我想我的.NET C#或VB.NET代碼中運行它創建了一個標量函數。
我使用實體框架,我試圖映射它與功能映射,我沒有成功。 我不在乎用簡單的DbCommand做到這一點,問題是,我沒有得到任何結果(在實體類存在的功能):
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#或VB.NET中的文章將會很好用。
結果: SQLEXCEPTION: 'fn_GetUserId_Username' 不是可以識別的函數名。 – Shimmy 2009-06-29 03:13:31
@Shimmy:指定所有者的名稱,「dbo」 - >「select dbo.fn_GetUserId_Username(@Username)」 – Sung 2009-06-29 03:20:23