gpt4 book ai didi

c# - 在 ASP.NET Core 3.1/5.0 中调用存储过程

转载 作者:行者123 更新时间:2023-12-02 16:08:52 29 4
gpt4 key购买 nike

我有一个简单的 ASP.NET Core 服务,它需要与数据库进行一次交互。它调用一个返回 BigInt/long 的存储过程。

为此启动所有的东西来运行 Entity Framework Core 似乎有点矫枉过正。特别是因为 EF Core 不能很好地执行存储过程(截至我上次检查时)。

是否有仅 .NET Core 的方法来调用和获取存储过程的结果(不使用 EF Core 或其他框架)?

最佳答案

像这样的东西是你需要的最低限度。显然,您可能希望将连接字符串存储在配置文件中。

await using DbConnection connection = new SqlConnection("Connection_String");
await connection.OpenAsync();
await using var command = connection.CreateCommand();

command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "Stored_Proc_Name";
// Declare any parameters here
var p = command.CreateParameter();
p.ParameterName = "IsCool";
p.Value = true;
p.DbType = System.Data.DbType.Boolean;
command.Parameters.Add(p);

var result = await command.ExecuteScalarAsync();
if (result == null) {
throw new Exception("Bad");
}

long numValue = (long) result;

关于c# - 在 ASP.NET Core 3.1/5.0 中调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68598086/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com