从数据库中检索一个值 - ADO.NET C#

Retrieve one value from database - ADO.NET C#

我在执行 ExecuteScalar 命令时遇到以下错误:

System.Data.SqlClient.SqlException: Invalid column name 'GU1'

代码如下。 table 称为 Outcodes,第 1 列是 Outcode,第 2 列是 Postcode。我正在尝试 return 邮政编码 = GU1 的外码值。我不确定为什么它正在寻找 GU1 作为列名而不是列中的值?我是不是很傻?非常感谢任何帮助 - 真的坚持这个!

String sqlpostcodetooutcode = "select outcode from Outcodes where postcode = GU1";
SqlCommand sqlcompostcodettooutcode = new SqlCommand(sqlpostcodetooutcode, con);

con.Open();

try
{
     Object outcode = sqlcompostcodettooutcode.ExecuteScalar();
     String Outcode = outcode.ToString();
     Response.Write(Outcode);
}
catch { }
finally
{
     con.Close();
}

您的查询中 GU1 周围缺少单引号 (')