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

prenom+" "+nom


但是,当我执行以下命令时,返回的值是

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()仅返回该行的第一列,因此您必须在查询本身中串联您的名称。

10-06 05:39