如何检索ADO.NET SqlCommand的结果?

编程入门 行业动态 更新时间:2024-10-10 00:27:41
本文介绍了如何检索ADO.NET SqlCommand的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,现在我真的很累还是很胖,但是我似乎找不到答案

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的结果?

本文发布于:2023-11-09 11:48:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ADO   NET   SqlCommand

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!