尝试通过c#运行sql时抛出异常#

时间:2018-01-18 15:47:54

标签: c# sql

我正在尝试通过c#运行数据库查询。我试图将一个参数传递给我的sql语句,但我得到一个异常,说在@Agent_ID附近无效。  我的代码就像这样

SqlCommand command = new SqlCommand("Select Csr_DISBURSEMENTDATE, Csr_AGENTNUMBER, Csr_TOTCURREARNINGS, Csr_MISCADJUSTMENTS, Csr_YTDTOTALCOMM, Csr_PAYMENTMETHOD From Cm_Opt_Csr_CommStatement_S " +
                        "inner join Cm_Opt_Con_Contract_S on Con_WritingCode = Csr_AgentNumber" +
                        "inner join Cm_Opt_Agt_Agent_S on agt_ID = Con_AgentID" +
                        "where Agt_ID = @AgentID");
                    command.Parameters.AddWithValue("@AgentID", Con_agentID);
                    command.Connection = conn;
                    SqlDataReader rdr = null;
                    rdr = command.ExecuteReader();

Con_agentID是一个guid,在数据库表中,它映射到的列是唯一的。我被困在这一点上。有人可以指出语法中的错误。 引发的异常是

System.Data.SqlClient.SqlException: 'Incorrect syntax near 'Agt_ID'.'

1 个答案:

答案 0 :(得分:1)

当你继续下一行时,你会在单词之间缺少空格。

SqlCommand command = new SqlCommand("Select Csr_DISBURSEMENTDATE, Csr_AGENTNUMBER, Csr_TOTCURREARNINGS, Csr_MISCADJUSTMENTS, Csr_YTDTOTALCOMM, Csr_PAYMENTMETHOD From Cm_Opt_Csr_CommStatement_S " +
                        "inner join Cm_Opt_Con_Contract_S on Con_WritingCode = Csr_AgentNumber " +
                        "inner join Cm_Opt_Agt_Agent_S on agt_ID = Con_AgentID " +
                        "where Agt_ID = @AgentID");
                    command.Parameters.AddWithValue("@AgentID", Con_agentID);
                    command.Connection = conn;
                    SqlDataReader rdr = null;
                    rdr = command.ExecuteReader();