【发布时间】:2020-03-08 20:45:36
【问题描述】:
在我的ExecuteScalar 命令点出现以下错误:
System.Data.SqlClient.SqlException:列名“GU1”无效
代码如下。该表称为Outcodes,第1 列是Outcode,第2 列是Postcode。我正在尝试返回 postcode = GU1 的 outcode 值。我不确定它为什么要寻找 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();
}
【问题讨论】:
-
"SELECT outcode FROM Outcodes WHERE postcode = 'GU1'"注意单引号。
标签: c# sql-server ado.net