插入语法句子有问题如何解决?

时间:2019-01-25 09:48:44

标签: c# asp.net database insert

大家好,我正在网站上,我的插入语法句子有问题,谁能帮助我找到句子中存在的问题?

using (OleDbConnection sqlCon = new OleDbConnection(connectionStr))
            {
                sqlCon.Open();
                string query = "INSERT INTO SoccerAchievements (PlayerCode,TeamCode,Achievement,When) VALUES (@PlayerCode,@TeamCode,@Achievement,@When)";
                OleDbCommand sqlCmd = new OleDbCommand(query, sqlCon);
                sqlCmd.Parameters.AddWithValue("@PlayerCode", (AdminBook.FooterRow.FindControl("txtPlayerCodeFooter") as TextBox).Text.Trim());
                sqlCmd.Parameters.AddWithValue("@TeamCode", (AdminBook.FooterRow.FindControl("txtTeamCodeFooter") as TextBox).Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Achievement", (AdminBook.FooterRow.FindControl("txtAchievementFooter") as TextBox).Text.Trim());
                sqlCmd.Parameters.AddWithValue("@When", (AdminBook.FooterRow.FindControl("txtWhenFooter") as TextBox).Text.Trim());
                sqlCmd.ExecuteNonQuery();
                PopulateGridView();
                lblSuccessMessage.Text = "נתון חדש נוסף בהצלחה";
                lblErrorMessage.Text = "";
            }
        }
    }

我希望它插入到我的数据库中,并给我我的插入句子语法有问题的错误

1 个答案:

答案 0 :(得分:2)

根据precisionWHEN用作保留字,因此需要将其包装在方括号内:

string query = "INSERT INTO SoccerAchievements (PlayerCode, TeamCode, Achievement, [When]) 
                VALUES (@PlayerCode, @TeamCode, @Achievement, @When)";

此外,您需要使用Add()而不是AddWithValue()来提供参数值。