如何在.NET中使用SQL用户定义函数?
我在数据库中创建了一个标量函数,
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 来做到这一点,问题是我没有得到任何结果(该函数存在于 Entities 类中):
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 发表帖子。
I created a scalar function in the DB
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
Now I want to run it within my .NET C# or VB.NET code.
I use Entity Framework, I tried to map it with function mapping and I did not success.
i don't care to do it with simple DbCommand, the problem is that I get no results (the function exists in the Entities class):
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;
}
Is there any solution?
Posts in either C# or VB.NET will be welcommed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,听起来正确的方法是使用实体框架的功能来定义 .NET 函数并将其映射到您的 UDF,但我想我明白为什么您没有得到当您使用 ADO.NET 执行此操作时,您会得到预期的结果 - 您告诉它您正在调用一个存储过程,但实际上您正在调用一个函数。
尝试这个:
It sounds like the right way in this case is to use the functionality of the entity framework to define a .NET function and map that to your UDF, but I think I see why you don't get the result you expect when you use ADO.NET to do it -- you're telling it you're calling a stored procedure, but you're really calling a function.
Try this:
这与上面的答案非常相似,但下面的代码允许您使用任意数量的参数和任意返回类型调用 UDF。 作为更通用的解决方案,这可能很有用。 这还没有经过彻底的测试...我认为 varchar 会有一些问题。
This is very similar to the above answer, but the below code allows you to call a UDF with any number of parameters and any return type. This might be useful as a more general solution. This also hasn't been tested thoroughly...I think it will have some problems with varchars.