mysql - SQL ExecuteScalar 2 返回值?

标签 mysql sql

我正在尝试从 SQL 查询返回 2 个值的串联。我在数据库中搜索 nom 和 prenom 并希望将它们返回为

prenom+" "+nom

但是,当我执行以下命令时,我在 returnValue 中得到的只是

nom

代码:

SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["SearchConnectionString"].ConnectionString;

SqlCommand searchCommand = new SqlCommand();
searchCommand.CommandText = "select nom,prenom from [reference].[dbo].[v_employe] where compagnie like @compagnie and no_employe like @num";
searchCommand.CommandType = CommandType.Text;
searchCommand.Connection = MyConnection;

SqlParameter p1 = new SqlParameter("@compagnie", this.REComboboxSearch.Value);
SqlParameter p2 = new SqlParameter("@num", this.RESearchId.Value);

searchCommand.Parameters.Add(p1);
searchCommand.Parameters.Add(p2);

MyConnection.Open();

returnValue = (String)searchCommand.ExecuteScalar();

MyConnection.Close();

谢谢!

最佳答案

使用

searchCommand.CommandText = "select nom + ' ' + prenom as c_nom from [reference].[dbo].[v_employe] where compagnie like @compagnie and no_employe like @num";

而不是在一列中获取串联名称。

方法 ExecuteScalar() 仅返回该行的第一列,因此您必须在查询本身中连接您的姓名。

关于mysql - SQL ExecuteScalar 2 返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919164/

相关文章:

mysql - n :m relation with many records that are just 1m correctly approach

c++ - 不能包含 mysql

c# - 比较巨大的 ASCII 文件

sql - SSIS部署: The task <task name> cannot run on this edition of integration services.需要更高级别的版本

php - mod_rewrite 我错过了什么?

php - MySql 数据库中的文件名搜索

mysql - 备份用户数据库的Shell脚本

Mysql 存储读取 true 或 false 的 bool 值

mysql - SQL - 如果相同的记录不在表 Y 中,则从表 X 中删除记录

用于读取特定表、列的 sql 命令