vb.net 检查表是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44583355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a table is empty or not
提问by Niloy
I am making a vb.net project. In one form, I want it to work like if user presses a button it first checks if a table(Built with SQL Server) is empty or not. If it is empty it will open another form otherwise Resume functioning. How to check if the table is empty or not. Thanks.
我正在制作一个 vb.net 项目。在一种形式中,我希望它像用户按下按钮一样工作,它首先检查表(使用 SQL Server 构建)是否为空。如果它是空的,它将打开另一个表单,否则恢复运行。如何检查表是否为空。谢谢。
回答by Arion
If you are after a sql statement that are check if there any rows in a table. THen you can do something like this:
如果您在执行检查表中是否有任何行的 sql 语句之后。然后你可以做这样的事情:
SELECT
(
CASE WHEN NOT EXISTS(SELECT NULL FROM yourTable)
THEN 1
ELSE 0
END
) AS isEmpty
回答by Osaf Malik
You can execute a SQL query to find the row count of your required table and then based on that count you can apply your logic by using conditional commands like If[...]Else:
您可以执行 SQL 查询来查找所需表的行数,然后根据该计数,您可以使用条件命令应用您的逻辑,例如If[...]Else:
Dim count As Int16
con.open()
query = "select count(*) from requiredTable"
cmd = New SqlCommand(query, con)
count = Convert.ToInt16(cmd.ExecuteScalar())
con.Close()
回答by Serg
Alternatively
或者
SELECT TOP(1) 1 FROM MyTable
and in your vb code check the number of rows returned ( 0 rows = table is empty)
并在您的 vb 代码中检查返回的行数(0 行 = 表为空)

