我试图从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();
谢谢!
答案 0 :(得分:1)
使用
searchCommand.CommandText = "select nom + ' ' + prenom as c_nom from [reference].[dbo].[v_employe] where compagnie like @compagnie and no_employe like @num";
而不是只在一列中获取连接名称。
方法ExecuteScalar()
仅返回行的第一列,因此您必须在查询本身中连接您的名称。