好吧,现在我真的很累还是很胖,但是我似乎找不到答案
Ok either I'm really tired or really thick at the moment, but I can't seem to find the answer for this
我正在使用ASP.NET,我想查找表中的行数.
I'm using ASP.NET and I want to find the amount of rows in my table.
我知道这是SQL代码:select count(*) from topics,但是如何将它显示为数字呢?
I know this is the SQL code: select count(*) from topics, but how the HECK do I get that to display as a number?
我要做的就是运行该代码,如果它= 0,则显示一件事,但如果它大于0,则显示另一件事.请帮忙吗?
All I want to do is run that code and if it = 0 display one thing but if it's more than 0 display something else. Help please?
这是我到目前为止所拥有的
This is what I have so far
string selectTopics = "select count(*) from topics"; // Define the ADO.NET Objects SqlConnection con = new SqlConnection(connectionString); SqlCommand topiccmd = new SqlCommand(selectTopics, con); if (topiccmd == 0) { noTopics.Visible = true; topics.Visible = false; }但是我知道我错过了严重的错误.我一直在寻找年龄,但找不到任何东西.
but I know I'm missing something seriously wrong. I've been searching for ages but can't find anything.
PHP非常简单. :)
PHP is so much easier. :)
推荐答案请注意,必须先打开连接并执行命令,然后才能访问SQL查询的结果. ExecuteScalar返回一个单个结果值(如果查询将返回多列和/或多行,则必须使用不同的方法).
Note that you must open the connection and execute the command before you can access the result of the SQL query. ExecuteScalar returns a single result value (different methods must be used if your query will return an multiple columns and / or multiple rows).
请注意使用using构造,该构造将安全地关闭并释放连接.
Notice the use of the using construct, which will safely close and dispose of the connection.
string selectTopics = "select count(*) from topics"; // Define the ADO.NET Objects using (SqlConnection con = new SqlConnection(connectionString)) { SqlCommand topiccmd = new SqlCommand(selectTopics, con); con.Open(); int numrows = (int)topiccmd.ExecuteScalar(); if (numrows == 0) { noTopics.Visible = true; topics.Visible = false; } }更多推荐
如何检索ADO.NET SqlCommand的结果?
发布评论