如何使用ExecuteScalar检索多个列? [英] How to retrieve more than one column using ExecuteScalar?

查看:37
本文介绍了如何使用ExecuteScalar检索多个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ExecuteScalar 获得一列:

 cmd.commandtext = "select rodeuser from customer_db_map";
    string rodecustomer = cmd.executescalar;

但是我需要获得不止一列,例如:

But I need to get more than one column, e.g.:

 cmd.commandtext = "select rodeuser,username,password from customer_db_map";

我需要字符串中的每一列:

I need each column in a string:

 string rodecustomer = cmd.ExecuteScalar();
 string username= cmd.ExecuteScalar();
 string password = cmd.ExecuteScalar();

但这是不可能的.如何实现的?

But that is not possible. How is this achieved?

推荐答案

ExecuteScalar 执行查询,并返回查询返回的结果集中第一行的第一列.其他列或行将被忽略.

ExecuteScalar executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

要实现此目的,您需要使用 SqlCommand.ExecuteReader 方法

To achecive this you need to use SqlCommand.ExecuteReader Method

这篇关于如何使用ExecuteScalar检索多个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆