例如,我有一个名为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: 1I 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;更多推荐
发布评论