使用C#执行SQLCommand时出错

时间:2011-09-27 08:46:17

标签: c# visual-studio-2010 ado.net sql-server-ce

我使用C#(Visual Studio 2010)对ADO.NET很陌生。使用LinqToSql不是一个选项,因为底层数据库是Compact Edition 3.5(不幸)。

下面显示的代码我得到一个错误:“解析查询时出错。[令牌行号= 2,令牌行偏移= 38,错误令牌= AgentName]”

有人可以告诉我我做错了吗?

 using (SqlCeConnection oConn = new SqlCeConnection(connectionstring))
        {
            string strSql = @"select 
                        a.name as 'AgentName',
                        t.description as 'JobType',
                        s.description as 'Status',
                        count(j.statusid) as 'Count'
                    from 
                        jobs as j 
                            inner join agents as a on j.agentid = a.id
                            inner join statusdictionary as s on j.statusid = s.id
                            inner join jobtypedictionary as t on j.jobtypeid = t.id
                    where 
                        convert(datetime,starttime,0) between @FirstDate and @LastDate 
                        AND j.JobTypeID = @JobTypeID AND j.AgentID = @AgentID
                    group by 
                        s.description, 
                        t.description, 
                        a.name order by a.name,     
                        t.description";
            SqlCeCommand oCmd = new SqlCeCommand(strSql, oConn);

            SqlCeParameter fdparam = new SqlCeParameter();
            fdparam.ParameterName = "@FirstDate";
            fdparam.Value = firstdate;
            oCmd.Parameters.Add(fdparam);

            SqlCeParameter ldparam = new SqlCeParameter();
            ldparam.ParameterName = "@LastDate";
            ldparam.Value = lastdate ;
            oCmd.Parameters.Add(ldparam);

            SqlCeParameter JIDparam = new SqlCeParameter();
            JIDparam.ParameterName = "@JobTypeID";
            JIDparam.Value = jobtypeid;
            oCmd.Parameters.Add(JIDparam);

            SqlCeParameter AIDparam = new SqlCeParameter();
            AIDparam.ParameterName = "@AgentID";
            AIDparam.Value = jobtypeid;
            oCmd.Parameters.Add(AIDparam);

            oConn.Open();
            SqlCeDataReader oReader = oCmd.ExecuteReader();

2 个答案:

答案 0 :(得分:4)

我认为您需要丢失'AgentName'周围的报价。

答案 1 :(得分:1)

将单引号''更改为方括号[]

  

a.name为[AgentName],