gpt4 book ai didi

c# - 检查 View 是否存在

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:04 27 4
gpt4 key购买 nike

我想以编程方式通过我的 Windows 窗体检查 View 是否存在,如果存在,则将其删除。我知道如何在 SQL Server 中执行此操作,但以前从未在 C# 中尝试过。我正在使用这种语法,但不能完全弄清楚它的 exat 语法(或者它是否正确)。

using (var command1 = connection.CreateCommand())
{
command1.CommandText = "If Object_ID('ServerName.dbo.ViewName', 'V') IS NOT NULL DROP VIEW 'ServerName.dbo.ViewName'
}

最佳答案

为什么不查询sys.views 目录 View 。下面的代码片段。

bool exists;
var cmd = new OdbcCommand(
"select case when exists((select * from sys.views where name ='" +
viewName + "')) then 1 else 0 end");
exists = (int)cmd.ExecuteScalar() == 1;

关于c# - 检查 View 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23252870/

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