查询未按预期工作

时间:2014-05-06 14:12:14

标签: c# sql-server

我遇到以下问题:在C#中执行查询

我有以下方法执行简单查询:

    public List<DataModel.Vulnerability.VulnerabilitySolution> getVulnerabilitySolutionsList(int vulnId)
    {
        List<DataModel.Vulnerability.VulnerabilitySolution> result = new List<Vulnerability.VulnerabilitySolution>();

        System.Data.Common.DbCommand command;
        command = _connection.CreateCommand();
        command.Connection = this._connection;

        _strSQL = "VS.* FROM VulnerabilityAlertDocument_VulnerabilitySolution VAD_VS"
                  + " INNER JOIN VulnerabilitySolution VS ON VAD_VS.VulnerabilitySolutionId = VS.Id"
                  + " WHERE VAD_VS.VulnerabilityAlertDocumentId = @VULNID ";

        addParameter(command, "@VULNID", vulnId);

        command.CommandText = _strSQL;
        _dt = fillDataTable(command);

        DataModel.Vulnerability.VulnerabilitySolution vulnSolution;

        foreach (DataRow row in _dt.Rows)
        {
            vulnSolution = new DataModel.Vulnerability.VulnerabilitySolution(row);
            result.Add(vulnSolution);
        }

        return result;
    }

问题在于,当尝试执行此行时: _dt = fillDataTable(command); 会引发以下异常:'*'附近的语法不正确

可能是什么问题?如果我尝试在SQL Server中执行相同的查询(替换参数的值),它可以正常工作

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:8)

您需要在VS。*

之前添加单词select
  _strSQL = "SELECT VS.* FROM VulnerabilityAlertDocument_VulnerabilitySolution VAD_VS"
                  + " INNER JOIN VulnerabilitySolution VS ON VAD_VS.VulnerabilitySolutionId = VS.Id"
                  + " WHERE VAD_VS.VulnerabilityAlertDocumentId = @VULNID ";