我正在尝试从 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/