gpt4 book ai didi

C# - 确定存储过程是否存在

转载 作者:行者123 更新时间:2023-12-02 17:42:47 24 4
gpt4 key购买 nike

<分区>

我编写了一个 DbContext 扩展来尝试确定存储过程是否存在于其关联的数据库中。

    public static bool StoredProcedureExists(this DbContext input, string name)
{
int exists = input.Database.ExecuteSqlCommand(string.Format("SELECT TOP 1 * FROM [sys].[objects] WHERE [type_desc] = 'SQL_STORED_PROCEDURE' AND [name] = '{0}';", name));

//return true; // if it exists, else false
}

问题是:无论存储过程 name 是否存在,我的 exists 变量(从 ExecSqlCommand 返回)总是包含 ' -1'。所以我无法确定存储过程是否在数据库中。

在 SQL Server Management Studio 中执行生成的查询按预期工作,如果存储过程存在则返回一行,如果不存在则不返回任何行。

有没有人知道如何实现这一点(以编程方式确定数据库中是否存在存储过程)?

谢谢罗布

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