从SELECT查询返回多个值(Return multiple values from a SELECT query)

编程入门 行业动态 更新时间:2024-10-24 14:27:10
从SELECT查询返回多个值(Return multiple values from a SELECT query)

例如,我有一个名为dbuser的数据库:

username: teste password: xxxx isonline: 1 username: teste2 password: xxxx isonline: 1

我以为这个查询:

"SELECT username FROM dbuser WHERE (isonline ='1')"

会返回teste和teste2,但是当我在MessageBox中询问结果时,teste和teste2都在线,它只显示teste,但是当我关闭teste连接时,它会在MessageBox中显示teste2。 我猜它只返回第一行给我,所以我怎么能得到所有的价值?

这是方法代码:

public static string GetOnline() { string listaOnline; listaOnline = ExecuteQuery("SELECT * username FROM dbuser WHERE (isonline ='1')").ToString(); return listaOnline; }

我把它显示为MessageBox.Show(DbManager.GetOnline());

For example i have a database called dbuser:

username: teste password: xxxx isonline: 1 username: teste2 password: xxxx isonline: 1

I thought that this query:

"SELECT username FROM dbuser WHERE (isonline ='1')"

would return both teste and teste2, but when i ask the result for example in a MessageBox, with both teste and teste2 online, it only shows teste, but when i close the teste connection then it appears teste2 in the MessageBox. Im guessing its only returning the first row to me, so how can i get all the values?

This is the method code:

public static string GetOnline() { string listaOnline; listaOnline = ExecuteQuery("SELECT * username FROM dbuser WHERE (isonline ='1')").ToString(); return listaOnline; }

and I show it as MessageBox.Show(DbManager.GetOnline());

最满意答案

这应该以最快的方式为您提供所需的字符串列表。 reader.GetString(0)表示从索引为0的列中获取sting值(所以第一个)。

List<string> result = new List<string>(); using (SqlConnection connection = new SqlConnection(databaseConnectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { command.CommandType = CommandType.Text; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add(reader.GetString(0)); } reader.Close(); } command.Cancel(); } } return result;

This should give you a list of strings that you want in the fastest manner. reader.GetString(0) means that you take a sting value from column with index 0 (so the first one).

List<string> result = new List<string>(); using (SqlConnection connection = new SqlConnection(databaseConnectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { command.CommandType = CommandType.Text; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add(reader.GetString(0)); } reader.Close(); } command.Cancel(); } } return result;

更多推荐

本文发布于:2023-07-14 20:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   Return   SELECT   query   values

发布评论

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

>www.elefans.com

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