我正在使用C#将信息从一个Access DB传输到另一个。使用以下代码生成错误,但通过使用相同的值将查询放入Access,我没有得到任何错误。谁能看到发生了什么?
string sqlInfaction = "INSERT INTO AquiredInfractions" +
"(AgentID, DateID, InfractionID, STime, Durration, Exception) " +
"SELECT " +
"@ID AS AgentID, (SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @DT) AS DateID, " +
"I.InfractionID, @ST AS STime, @DR AS Durration, @E AS Exception " +
"FROM " +
"InfractionTypes AS I " +
"INNER JOIN " +
"Groupings AS G " +
"ON I.GroupingID = G.GroupingID " +
"WHERE " +
"G.GroupingTitle = @NAME " +
"AND " +
"I.MinDur <= @DR1 AND I.MaxDur >= @DR2;";
OleDbCommand InfCmd = null;
然后设置:
string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\New.Employee\Documents\UserMan2.accdb";
conn = new OleDbConnection(conString);
InfCmd = new OleDbCommand(sqlInfaction, conn);
InfCmd.Parameters.Add("@ID", OleDbType.Integer);
InfCmd.Parameters.Add("@DT", OleDbType.Date);
InfCmd.Parameters.Add("@ST", OleDbType.SmallInt);
InfCmd.Parameters.Add("@DR", OleDbType.SmallInt);
InfCmd.Parameters.Add("@E", OleDbType.Boolean);
InfCmd.Parameters.Add("@NAME", OleDbType.VarWChar);
InfCmd.Parameters.Add("@DR1", OleDbType.SmallInt);
InfCmd.Parameters.Add("@DR2", OleDbType.SmallInt);
在传递函数中:
InfCmd.Parameters["@ID"].Value = inf.AgentID;
InfCmd.Parameters["@DT"].Value = inf.date;
InfCmd.Parameters["@ST"].Value = inf.startTime;
InfCmd.Parameters["@DR"].Value = inf.durration;
InfCmd.Parameters["@E"].Value = inf.exception;
InfCmd.Parameters["@NAME"].Value = inf.infract;
InfCmd.Parameters["@DR1"].Value = inf.durration;
InfCmd.Parameters["@DR2"].Value = inf.durration;
InfCmd.ExecuteNonQuery();
在测试中,我逐步完成了运行代码,使用上述参数中的值来测试Access中的查询。
通过quickwatch实现sqlInfaction的价值:
sqlInfaction "INSERT INTO AquiredInfractions(AgentID, DateID, InfractionID, STime, Durration, Exception)
SELECT @ID AS AgentID, (SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @DT) AS DateID,
I.InfractionID, @ST AS STime, @DR AS Durration, @E AS Exception FROM InfractionTypes AS I
INNER JOIN Groupings AS G ON I.GroupingID = G.GroupingID WHERE G.GroupingTitle = @NAME
AND I.MinDur <= @DR1 AND I.MaxDur >= @DR2;" string
例外情况如下:
Message: Syntax error in INSERT INTO statement.
Error Code: -2147217900
如果抛出的异常中有特定内容,请告诉我,但这是整个消息。
答案 0 :(得分:1)
在玩完之后,我发现[]
周围的答案丢失Exception
。正确的查询显示为:
string sqlInfaction = "INSERT INTO AquiredInfractions" +
"(AgentID, DateID, InfractionID, STime, Durration, [Exception]) " +
"SELECT " +
"@ID AS AgentID, (SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @DT) AS DateID, " +
"I.InfractionID, @ST AS STime, @DR AS Durration, @E AS [Exception] " +
"FROM " +
"InfractionTypes AS I " +
"INNER JOIN " +
"Groupings AS G " +
"ON I.GroupingID = G.GroupingID " +
"WHERE " +
"G.GroupingTitle = @NAME " +
"AND " +
"I.MinDur <= @DR1 AND I.MaxDur >= @DR2;";