0
string query = "select a.Name,a.Add from GroupDetails a join General b on a.ID=b.Id where b.AccCode='" + label1.text + "'";
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter daName = new OleDbDataAdapter(cmd);

OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{

    txtName.Text = dr["a.Name"].ToString();
    txtAdd.Text = dr["a.Add"].ToString();
}

有显示异常

FROM 子句中的语法错误

4

3 回答 3

1

如果它明确提到from,那么我的猜测是 SQL 后端不支持别名(a)。但是,存在多个问题:

  • 别名
  • sql 连接
  • 不必要的适配器
  • 读取的列不正确

我建议尝试:

const string query = "select GroupDetails.Name,GroupDetails.Add from GroupDetails join General on GroupDetails.ID=General.Id where General.AccCode=@accCode";

using(var cmd = new OleDbCommand(query, con))
{
    cmd.Parameters.AddWithValue("accCode", label1.text);
    using(var dr = cmd.ExecuteReader())
    {
        txtName.Text = (string)dr["Name"];
        txtAdd.Text = (string)dr["Add"];
    }
}
于 2013-08-02T07:19:10.290 回答
1

我确定您的查询不是SQL Server因为它在 SQL Server 查询窗口中工作正常。它可能是Access SQLor My SQL,您必须明确指定left join, right joinor inner join。我想你inner join在这种情况下想要:

string query = "SELECT a.Name,a.Add FROM GroupDetails a INNER JOIN General b ON a.ID=b.Id WHERE b.AccCode='" + label1.text + "'";
于 2013-08-02T07:35:24.573 回答
0

Add是SQL的Keyword,可以像a.[Add]这样的字段[]

string query = "select a.Name,a.[Add] from GroupDetails a join General b on a.ID=b.Id where b.AccCode='" + label1.text + "'";
于 2013-08-02T07:22:06.847 回答