【问题标题】:Retrieve one value from database - ADO.NET C# [closed]从数据库中检索一个值 - ADO.NET C# [关闭]
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 谢谢两位!这么简单的错误浪费了这么多时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 2017-05-04
相关资源
最近更新 更多